KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
How do rails association methods work? Lets consider this example class User < ActiveRecord::Base has_many :articles end class Article < ActiveRecord::Base belongs_to :user end Now I can do something like @user = User.find(:first) @user.articles This fetches me articles belonging to that user. So far so good. Now further I can go ahead and do a find on these articles with some conditions. @user.articles.find(:all, :conditions => {:sector_id => 3}) Or simply declare and associations method like class User < ActiveRecord::Base has_many :articles do def of_sector(sector_id) find(:all, :conditions => {:sector_id => sector_id}) end end end And do @user.articles.of_sector(3) Now my question is, how does this find work on the array of ActiveRecord objects fetched using the association method? Because if we implement our own User instance method called articles and write our own implementation that gives us the exact same results as that of the association method, the find on the fetch array of ActiveRecord objects wont work. My guess is that the association methods attach certain properties to the array of fetched objects that enables further querying using find and other ActiveRecord methods. What is the sequence of code execution in this this case? How could I validate this?
Tags (comma-separated)
Save Edits
Cancel