Ruby on Rails - Models and Relationships Table

I am a noob, starting with rubies on rails and trying to figure out models. Pretty familiar with databases and wanted to understand when to create models for relationships?

For example, I have a table of tables and user gadgets. Each user can have several gadgets. I want to keep this relationship in a relationship table with user_id and gadget_id. I created two tables using rails generate model User and rails generates a Gadget cmd model.

Question. Will I be able to create another model called users_gadgets? Is the naming convention correct? I saw a lot of documentation on how to create associations (has_many, belongs_to), but don’t understand when I need to create a model?

Do I need to create models every time I want to store relationships in a database and then run my migrations?

+7
source share
4 answers

A relationship model only makes sense for an N-to-N relationship, as you described it. When you decide to use has_and_belongs_to_many, you do not need to create a join model, but you still have to create a join table through migration. The naming convention is gadgets_users because the "g" is before the "u" in the alphabet.

If you selected has_many: through, you can save additional information in the connection table using the GadgetUser join model. For example, if you want to record that the gadget belonged to the user for a given time, then this information logically refers to the “connection” between the gadget and the user, so you should store it in the connection table.

+5
source

You can do this in one of two ways.

1) has_and_belongs_to_many in this case you just need a table (without model) gadgets_users with user_id gadget_id, rails likes table names to be multiple in alpha order

2) has_many: through , it’s not “normal”, it has a lot, in this case you need a model (you can name whatever you like) I prefer it one by one because it allows you to add additional attributes to the relations if necessary

see the manual, it explains the work better than I can http://guides.rubyonrails.org/association_basics.html#choosing-between-has_many-through-and-has_and_belongs_to_many

also - railscasts - http://railscasts.com/episodes/47-two-many-to-many

Do I need to create models every time I want to store relationships in a database and then run my migrations? Yes, most of the time

+4
source

I believe that the most useful way to think about it is to think about models related to your application logic and data related tables. So say, for example, your user has a name and a height and weight. These are user attributes and are stored in a table. They are available through model attributes. But the user may also have a body mass index calculated by height and weight. This is a model method, but it is indirectly associated only with the database.

Personally, I would always create a model for the database table. This is easier to do this way, to simplify writing a clean ruby ​​code without a lot of SQL statements, and if at some point in the future you want to add logic to the model, this is easy to do.

+1
source

Install gem install alien

add the following line to the gem file

gem 'alien'

after viewing the following link

https://github.com/matthuhiggins/foreigner

0
source

All Articles