Correct many-to-many implementation in Ruby on Rails?

Newbie question, beware! I would like to implement the basic many-to-many relationship in Rails, and I'm trying to figure out which approach is considered the most “rail-based”. In a traditional database other than ActiveRecord, I simply created two tables and a connection table and wrote a bunch of logic to make sure that all three tables were taken into account when performing operations on any of them.

This is my first time using ORM, and so I'm trying to figure out if ActiveRecord can simplify the process for you, perhaps without requiring you to manually create a connection table.

Railscasts seems to be a respected source of Rails wisdom, are there two ways in this great Rails style, or can I do better? - http://railscasts.com/episodes/47-two-many-to-many

+4
source share
3 answers

There are basically two ways: has_and_belongs_to_many ( habtm ) and has_many with the: through option, which points to another connection. Both require joining tables; the latter is what we call the join model, because you usually add additional information to the connection.

For example, consider an application with a user model that has bookmarking sites. One way would be to implement it as a habtm relationship

 class User < ActiveRecord::Base has_and_belongs_to_many :sites end class Site < ActiveRecord::Base has_and_belongs_to_many :users end user.sites << Site.find(...) 

This simulation will also require the creation of a sites_users table, in which there will be no primary key.

The problem with this is that you most likely want to keep additional information about it, so that you can go with the connection model, in this case Bookmark:

 class User < ActiveRecord::Base has_many :bookmarks has_many :sites, :through => :bookmarks end class Site < ActiveRecord::Base has_many :bookmarks has_many :users, :through => :bookmarks #edit: adding validation for requiring at least one bookmark validate_before_create :at_least_one_bookmark private def at_least_one_bookmark errors.add_to_base("requires at least one bookmark") unless bookmarks.count > 0 end end class Bookmark < ActiveRecord::Base belongs_to :user belongs_to :site end user.bookmarks.create(:site => Site.find(...) ) 

A more common pattern is the union model approach for its versatility and better modeling, although habtm is still used somewhat. They are just so two-dimensional that you really need to study what you are doing and make sure that there is no richer behavior that also needs to be modeled.

+10
source

Railscasts is the most reliable source of wisdom :) Ryan Bates will teach a true "rail track" to solve problems in rubies on rails applications. You have to do it like this.

But if you want to know more about associations, please follow this link http://guides.rubyonrails.org/association_basics.html

Happy coding!

+1
source

As already mentioned, Rails makes this pretty easy using has_and_belongs_to_many or has_many though .

I would not say that one of them is “more right” than the other, it just depends on what you need to do, namely around how you need to manipulate the combined models. The solution between them is discussed here .

(All of these links are from an already provided doc).

0
source

All Articles