Rails relationship -


I'm trying to find out some of the railings relationships I already posted a question about a specific item But I did not really understand what has been done in underlying DB.

I have a project model and a customer model one project related_to: client = & gt; I need to manually add client_id (with migration) to the Projects table.
a customer is_images: projects = & gt; I do not need to do anything in DB (no migration) project.client and client.projects are both available.

I have a group model and user model model code> me then a user_id and group_id points.

I do not really know the boundary between Rail and Relational Databases Why do I ever need to add foreign keys but like always?

I sometimes get lost :)

For this granular person, nothing has been done in the underlying DB, because has_many handled the relationship goes? Thanks and Regards,

Luke

one has_many & lt; - & gt; Assoication, you are defining that a customer is owned by a customer ( related_to ), so that many clients ( is ) Projects are for a project to determine which customer it belongs to, it should have a client_id column so that it can see it. This client_id column is used by the rail when you call the customer method, much more:

  Client.find (Project.client_id)   

How can you find a project's client? The client_id column is often referred to as the foreign key , because its unique identifier ("key") is not in a table whose root ("foreign") . Boom.

When you call in another way, it is to find all the projects of a customer, i.e. client.projects , the rail is equal to:

 project.find_all_by_client_id (client.id)   

It then records all projects that are associated with a particular client, Associations in the has_and_belongs_to_many field in the Projects table Example of groups, you are declaring that a user is is_or_blog_to: group .

Now if this is only a is_me: group , then the foreign key was in the groups table or if it was a related < / Code> goes to the user table. Good thing to remember: The foreign key always goes to the table of the model, which contains related_to .

You are also announcing that a group is is_ and_blogs_to_mem: user , and so we come to the same problem. We can not declare the key in the users' table because it There is no (because the user has several groups, you will need to collect all the group ID of the user) or group tables for the same reasons. / P>

This is the reason that we need to create has_and_belongs_to_many which is known as join table in this table, two and only two fields ( Both are foreign keys), one for the other side of the union and one for the other, to create this table, we will put it in the self.up method of migration: < Pre> create_table: groups_users ,: id = & gt; Do false T | T.integer: group_id t.integer: user_id end

Here are some things to note:

  1. The name of the table is two names of two alphabetically Association comes before GU and the name of the table is groups_users .
  2. In the : id option, which is given when the value of false generates a table without a primary key, an included table The primary key is not required because its purpose is to be included in other tables simultaneously.
  3. We have a group_id and user_id in the integer field, as we are on a related_to association.

    This table will keep track of which groups are users and vice versa. There is no need to define additional columns in the

    user or groups table because the table joining is in control.

Comments