Norm Scherer wrote:
> <html>
HTML mail to a mailing list. Yuck!
> I have three tables/models and I want to do a find on Reservations
> ordering the results by the names in Sitetype. Sql for tables and
> the models look like the following:
You might have a better mental model if you use ActiveRecord modeling
instead of raw SQL.
class Reservation < ActiveRecord::Base
belongs_to :space
end
class Space < ActiveRecord::Base
belongs_to :sitetype
end
class Sitetype < ActiveRecord::Base
end
> Whenever I try to include sitetype as in
> r=Reservation.find(:all, :include => [:space, :sitetype], :order =>
> "sitetypes.name asc")
> I get a complaint "Association named 'sitetype' was not found"
>
> Could someone who understands this better than I recommend an
> approach to do what is needed here?
I think you have a nested datastructure and so need to use that same
nesting when refering to it in the include.
Reservation.find(:all, :include => { :space => { :sitetype => {} } }, :order
=> "sitetypes.name asc")
Which I think can be reduced to this:
Reservation.find(:all, :include => { :space => :sitetype }, :order =>
"sitetypes.name asc")
Note that I didn't test this and so could have it wrong.
See the "Eager loading of associations" in this reference for more details:
http://rails.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html
Bob
--
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.