eager-loading
Rails eager loading and conditions
I have the following associations set up class bookinghdr belongs_to :agent end class bookingitem belongs_to :bookinghdr, :include => agent end So I was expecting to be able to do the following: named_scope :prepay, :include=>["bookinghdr"], :conditions => ["bookinghdr.agent.agenttype = 'PP'"] and in my controller do: b = Bookingitem.prepay But that gives me a ActiveRecord::StatementInvalid: Mysql::Error: Unknown column 'bookinghdr.agent.agenttype' However if I don't include the conditions clause then I get a recordset on which I can do: b = Bookingitem.prepay b[0].bookinghdr.agent.agenttype without any error!! I don't want to have to get all the records and then iterate over them to find the ones whose agent has a 'PP# flag. I was hoping that AR would do that for me. Anybody got any ideas on how to achieve this? Thanks in advance. Purvez
Your question shows that you have not yet fully understood how associations and named scopes work. Since I cannot tell from your question what parts aren't clear, I suggest you read the Association Basics guide at http://guides.rubyonrails.org/v2.3.11/association_basics.html. This should bring you up to speed regarding the concepts you want to implement. After you have read the guide it should all make sense.
Related Links
How to enable eager loading on a association in phpactiverecord?
Rails eager loading with conditions
Rails eager loading and conditions
Eager loading for globalize2 translations