Bob Proulx wrote:
Norm Scherer wrote:
  
<html>
    

HTML mail to a mailing list.  Yuck!
  
Sorry about that.  I wasn't thinking.
  
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.
  
I gave you both the sql and the models just to demonstrate that the database was correctly formed.
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")
  
That works like a charm!   I do not understand the :include => { :space => :sitetype } syntax.  What is that saying and why does it work?
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
  
I looked at eager loading in Agile Web Development and did not really understand it all (obviously).  I will look again at the api as recommended to see if that is any more understandable.
Bob

  
Thanks for the help.

Norm

--
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.

Reply via email to