Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need help to understand :source_type option of has_one/has_many through of Rails

In Rails 3.1, the documentation says

"4.2.2.13 :source_type

The :source_type option specifies the source association type for a has_one :through association that proceeds through a polymorphic association. "

I just read :source explanation but still dont get what source_type is used for?

like image 696
Kamilski81 Avatar asked Feb 29 '12 14:02

Kamilski81


People also ask

How would you choose between Belongs_to and Has_one?

They essentially do the same thing, the only difference is what side of the relationship you are on. If a User has a Profile , then in the User class you'd have has_one :profile and in the Profile class you'd have belongs_to :user . To determine who "has" the other object, look at where the foreign key is.

What is a polymorphic association in Rails?

In Ruby on Rails, a polymorphic association is an Active Record association that can connect a model to multiple other models. For example, we can use a single association to connect the Review model with the Event and Restaurant models, allowing us to connect a review with either an event or a restaurant.

What is association in ruby on Rails?

Association in Rails defines the relationship between models. It is also the connection between two Active Record models. To figure out the relationship between models, we have to determine the types of relationship. Whether it; belongs_to, has_many, has_one, has_one:through, has_and_belongs_to_many.


2 Answers

:source_type deals with associations that are polymorphic. That is to say, if you have a relationship like this:

class Tag < ActiveRecord::Base   has_many :taggings, :dependent => :destroy   has_many :books, :through => :taggings, :source => :taggable, :source_type => "Book"   has_many :movies, :through => :taggings, :source => :taggable, :source_type => "Movie" end  class Tagging < ActiveRecord::Base   belongs_to :taggable, :polymorphic => true   belongs_to :tag end  class Book < ActiveRecord::Base   has_many :taggings, :as => :taggable   has_many :tags, :through => :taggings end  class Movie < ActiveRecord::Base   has_many :taggings, :as => :taggable   has_many :tags, :through => :taggings end 

Then the source type allows you to make queries like this:

"Find me all of the books that have been tagged with the tag named 'Fun'"

tag = tag.find_by_name('Fun') tag.books 

Without source type, you wouldn't be able to do that, you could only get a collection of objects that were tagged with 'Fun'. If you only specificed source, it wouldn't know which kind of class the objects were, so you it wouldn't know which table in the DB to pull from. The source_type Informs it of which type of object you are trying to retreive.

This is taken from this blog post: http://www.brentmc79.com/posts/polymorphic-many-to-many-associations-in-rails

Hope it helps.

like image 86
TheDelChop Avatar answered Sep 24 '22 13:09

TheDelChop


☝🏼 this message is about the post above (I don't have enough reputation yet to add this as comment...)

Thanks @TheDelChop for that simple and perfect use case. I just propose to complete your perfect explanation with this simple schema which describe your user story. Just in case of. Thank you ! enter image description here

like image 25
v2lrf Avatar answered Sep 25 '22 13:09

v2lrf