I have a class like this:
class Project < ActiveRecord::Base
has_many :versions, :order => :position
end
In a controller action, I need to query for a project's versions. I have the project ID. I could simply do a
versions = Project.find(params[:project_id]).versions
However, this will first do a query to the "projects" table, which is unnecessary in my case. I could change it to
versions = Version.find( :conditions => { :project_id => params[:project_id } )
(or use a dynamic finder with the same results)
This doesn't use the additional attributes of the association (in this case, the ":order => :position"), so if I wanted to get exactly the same result as in the same example, I will have to write:
versions = Version.find( :conditions => { :project_id => params[:project_id },
:order => :position )
And here I'm no longer DRY since I'm repeating something (the ":order" option) that is already defined in the model.
Is there a way to use and follow the association defined in the model without having to load the parent object? I'm currently still using Rails 2.3.8.