I have a table countries and a model Country with the following columns id,cc_fips,cc_iso,tld, country_name with all the countries in the world listed.
I have a table users and model User with columns id, name, age...........
Every user has one country but one country has many users.
I want to create a table in the middle where i will register columns country_id, user_id.
A one to many association.
I named the table countryforusers and the model Countryforuser.
My migration to create the model:
class CreateCountryforusers < ActiveRecord::Migration
def change
create_table :countryforusers, {:id => false} do |t|
t.integer "country_id"
t.string "user_id", :null => false, :default => ""
t.timestamps
end
add_index("countryforusers","country_id")
end
end
My model User:
class User < ActiveRecord::Base
has_many :images
belongs_to :countryforuser
end
My model Countryforuser:
class Countryforuser < ActiveRecord::Base
has_many :users
end
However it doesn't work.
I have all the time a message undefined method `countryforusers' when trying to add one user for a country.
What am i doing wring?
Thank you very much.
Although each user has one country and one country many users, i had to use a many to many association, if i would put a join table in the middle. So the word many to many is a bit confusing this time.
Related
my logs contain in customDimensions multiple entries.
{"MessageType":"EventLog","Properties":"{\"Action\":\"Manual Trigger\",\"City\":\"New York\"}","AppBuild":"22"}
How do I extract the entries in "Properties" and put them in separate columns?
{\"Action\":\"Manual Trigger\",\"City\":\"New York\"}
Action
City
Manual Trigger
New York
For some reason, the entries in "Properties" do not have the same order. Sometimes "City" can be first.
I could extract properties, but this is only halfway
let events = customEvents
| extend properties = tostring(customDimensions["Properties"])
or
let events = customEvents
| extend properties = tostring(customDimensions.Properties)
Splitting the result could work, except, that the entries do not have the same order for each log entry.
you could use the bag_unpack() plugin.
for efficiency, make sure you specify the output schema (in this case (Action: string, City: string))
datatable(customDimensions:dynamic)
[
dynamic({"MessageType":"EventLog","Properties":"{\"Action\":\"Manual Trigger\",\"City\":\"San Francisco\"}","AppBuild":"21"}),
dynamic({"MessageType":"EventLog","Properties":"{\"Action\":\"Manual Trigger\",\"City\":\"New York\"}","AppBuild":"22"})
]
| extend properties = parse_json(tostring(customDimensions.Properties))
| evaluate bag_unpack(properties) : (Action: string, City: string)
Action
City
Manual Trigger
San Francisco
Manual Trigger
New York
I am getting an error
[BUILDER_UNEXPECTED_ERROR] Parent/Child View 'Dim_Parent_Child' without a single root member, at least one root member is mandatory. A Root member is defined with PARENT_ID = NULL or PARENT_ID = ID. location:
Please suggest me to how to define Root member with PARENT_ID = NULL or PARENT_ID = ID for Parent/Child Dimensions
One more thing, It is working fine with Full Load but raising error during incremental load.
The definition of this view (sorting Columns) is (ID, Parent).
I am using below table data as input to this "sort" view
ID,Name,Parent
0,Base,NULL
1,Customer Y,18
2,Funding Y,24
3,Credit Risk Sprd,22
4,Liquidity Sprd,24
5,Option Sprd,24
6,Funding Sprd,24
7,Custom Sprd,24
8,Early Termination Adjust,22
9,Risk Free Rate,19
18,Commercial ,22
19,Funding Center ,0
22,PC Contribution,0
23,Transfer Price PC,18
24,Transfer Price,19
25,Product Sprd,24
With incremental load this view does not work as it is not going to see all the rows: only new rows will are sent to the view and therefore the root will be missing.
Workaround: as it seems you're creating a dimension with this table (& view), you can set the incr. load strategy of this table to FULL_LOAD. Existing members won't be created.
Hope tha helps.
I am creating a domain-specific model which includes entities that have attributes whose original source or citation needs to be defined.
In graql for example:
define
"country" sub entity
has population;
"evidence" sub attribute datatype string;
"population" sub attribute datatype string
has evidence;
This seems to define an attribute of an attribute, and conceptually seems to make the meaning of the attribute dependent on a certain context, which is arguably better modelled as annotated "fact" entities with relationships to other entities.
What is the simplest way to model attributes like these without increasing complexity of the model?
Attributes of attributes
Attributes of attributes don't necessarily work as you might expect. It's important to remember that in Grakn there will be only one node in the graph for an attribute of a particular type with a particular value.
That is to say an attribute of type population value sixty million will only occur once in the knowledge graph.
If we change your schema slightly to add names for countries (also there's no need for single quotes around types):
define
country sub entity
has population,
has name;
name sub attribute datatype string;
evidence sub attribute datatype string;
population sub attribute datatype string
has evidence;
Then add two countries to the knowledge graph:
insert $uk isa country, has name 'UK', has population $p; $p 'sixty million' has evidence 'journal';
insert $fr isa country, has name 'France', has population $p; $p 'sixty million' has evidence 'wikipedia';
commit;
What we can see if we visualise it is that we can't tell the source of the population for each country separately, because both of the countries and both of the pieces of evidence are connected to the same population instance.
(Visualised in Grakn Workbase Visualiser)
Attributes of attributes make sense in a case like:
attribute phrase value Hi there! owning an attribute language value English. That is, the language attribute is referring to the value of the phrase attribute.
This means that if you want to record the source of an attribute you'll need to do things differently. I suggest three possible options. Note, that for each of the following three ideas population shouldn't own evidence for the reason mentioned. In the schema above population sub attribute datatype string has evidence; should become population sub attribute datatype string;
1. Implicit relationships
Under the hood Grakn has implicit relationships to implement attribute ownership, always autogenerated and prefixed with #has-, for example #has-population. We can attach attributes to these implicit relationships!
First delete the instances we inserted above (this will delete all entities and attributes in the graph, beware!):
match $x isa entity; $y isa attribute; delete $x, $y;
Then define that the implicit population attribute can own evidence and add examples:
define #has-population has evidence;
insert $uk isa country, has name 'UK', has population $p via $r; $p 'sixty million'; $r has evidence 'journal';
insert $fr isa country, has name 'France', has population $p via $r; $p 'sixty million'; $r has evidence 'wikipedia';
Now we're able to disambiguate the evidence for the UK's population from the evidence for France's population. We can query for this:
match $c isa country, has name $n, has population $p via $r;
$p 'sixty million'; $r has evidence $e; get $n, $e;
Result:
{$n val "France" isa name; $e val "wikipedia" isa evidence;}
{$n val "UK" isa name; $e val "journal" isa evidence;}
2. Relationships to implicit relationship
If the evidence is more complex than a single attribute, then it may be better modelled as a relationship, in which #has-population plays a role.
define
information-sourcing sub relationship,
relates sourced-information,
relates information-source;
#has-population plays sourced-information;
publication sub entity,
plays information-source;
insert $uk isa country, has name 'UK', has population $p via $r; $p 'sixty million'; $pub isa publication; $i(sourced-information: $r, information-source: $pub) isa information-sourcing;
insert $uk isa country, has name 'France', has population $p via $r; $p 'sixty million'; $pub isa publication; $i(sourced-information: $r, information-source: $pub) isa information-sourcing;
3. A normal relationship
Finally, you could create a relationship that links the population, country, and evidence, that avoids using implicit relationships if these seem too complex.
Conclusion
Which method to use depends on the domain you're modelling. In answer to your question, the first method adds the fewest additional elements to the schema.
I have a group, which I can attach to other modules. The group can have a parent group:
schema "groups" do
field :name, :string
field :deleted, :boolean
belongs_to :parent, Group
has_many :users_groups, UserGroup, foreign_key: :group_id
timestamps()
end
Via user_groups i Can attach users. Now I want to filter, if the user is allowed to see the attached module. I made a check, to see if the user is inside the attached group:
def get_visible_module(module, user_id) do
case module.group do
nil -> module
_ ->
case module.group.users_groups do
nil -> module
_ ->
val = Enum.filter(module.group.users_groups, fn(x)->
x.user_id == user_id
end)
case val do
[] ->
case false do
true -> module
false -> nil
end
_ -> module
end
end
end
end
This may be not the best code, but I am still learning, so improvements to this part are also welcome :)
Now my problem is to add a recursion to check, if the user_id is attached via user_group to a group, which is attached via the parent_id. I am stucked at this point. For the understanding: A module has a group attached. Only the user in the group or in the group attached via parent_id are allowed to see the module. Groups are structered as a tree, so I need to eager load the parent group and check if the user_group contains the user and check every parent-group too.
Hope it is understandable.
THX
Here is a rough skeleton which adapts the naming of your source code. It should give you an idea on how to make a recursion in elixir.
# Exit case when a module has no more parent
def get_visible_module(module, nil, user_id) do
user_in_groups?(module.group.users_groups, user_id)
end
# Case when the module has a parent_id
def get_visible_module(module, parent_id, user_id) do
# check the groups for user_id permission followed by the recusive part
user_in_groups?(module.group.users_groups, user_id) and get_visible_module(parent_module, parent_module.parent_id, user_id)
end
# checks if the user is in the group
defp user_in_groups?(users_groups, user_id) do
# check if the use is in one of the groups
true
end
As #bla already mentioned you should try to use pattern matching to clean up your code and reduce the nesting level of your code.
I want to sort data in repository based on the content of a specific field
For example i have an entity person with the fields role,fistName and lastName.
I would like to sort using ->orderBy('p.role', ???) and get a list ordered based on this order : professors then directors then teachers and at last students .
Example of my database:
Wanted result:
Ps: i can not use ASC or DESC since my sorting order is neither ASC nor DESC;
it is a custom order
Very strage scenario, you could use a CASE statement in order to assing a custom value for each fields then sort on him.
You could use the HIDDEN keyword.
As example take a look at this DQL:
SELECT p, CASE
WHEN p.role = "professor" THEN 1
WHEN p.role = "director" THEN 2
WHEN p.role = "teacher" THEN 3
WHEN p.role = "student" THEN 4
ELSE 99 END
AS HIDDEN mySortRule
FROM Bundle\Entity\Person p
ORDER BY mySortRule ASC
Hope this help