Julian Leviston
2006-Mar-15 12:41 UTC
[Rails] An Association Problem: AR::Base.find{:include} and n-deep association traversal
Dear List, I''m using Edge Rails. I have a Model with Items for purchase. Each Item is of a certain brand. Class Brand < ActiveRecord::Base end Class Item < ActiveRecord::Base belongs_to :brand end The problem domain is that I''d like to create a separate Inventory list of Items. This includes the date of entry into inventory and date of exit from inventory (ie sale). Because each Item can potentially be sold back into the system, and we want to maintain a historical record, this next class (which I''m choosing to call InventoryItem) is not just a simple list of items - each item may have many records in this list. But each invoice_item is only ever associated with ONE Item. Class InventoryItem < ActiveRecord::Base belongs_to :item end and Items gets modified to: Class Item < ActiveRecord::Base belongs_to :brand has_many :inventory_items end This is just fine and dandy, until I come to want to do searching with the AR::Base.find method... and I end up not being able to find based on brand. Obviously the :include => [] element of the options hash won''t work here, as I''ve tried, because it''s only looking at what I''m choosing to call two-deep association traversals. I have obviously got the option to simply use Ruby to do this, but I''d rather use find, obviously, because it''ll generate proper SQL and I''d say it''d be much faster. My quesiton is basically "Is there a way to do this elegantly using the find command and some option I''m not aware of as yet, or do I have to resort to using Ruby to do my pagination and limiting?". I''d like to be able to do stuff like InventoryItem.find (:all,''brand.name LIKE "%nike%") or somesuch. Thanks in advance, Julian.
Julian Leviston
2006-Mar-17 01:35 UTC
[Rails] Re: An Association Problem: AR::Base.find{:include} and n-deep association traversal
So I guess no one has a response to this? On 15/03/2006, at 11:41 PM, Julian Leviston wrote:> Dear List, > > I''m using Edge Rails. > > I have a Model with Items for purchase. Each Item is of a certain > brand. > > Class Brand < ActiveRecord::Base > end > > Class Item < ActiveRecord::Base > belongs_to :brand > end > > The problem domain is that I''d like to create a separate Inventory > list of Items. This includes the date of entry into inventory and > date of exit from inventory (ie sale). Because each Item can > potentially be sold back into the system, and we want to maintain a > historical record, this next class (which I''m choosing to call > InventoryItem) is not just a simple list of items - each item may > have many records in this list. But each invoice_item is only ever > associated with ONE Item. > > Class InventoryItem < ActiveRecord::Base > belongs_to :item > end > > and Items gets modified to: > > Class Item < ActiveRecord::Base > belongs_to :brand > has_many :inventory_items > end > > This is just fine and dandy, until I come to want to do searching > with the AR::Base.find method... and I end up not being able to > find based on brand. Obviously the :include => [] element of the > options hash won''t work here, as I''ve tried, because it''s only > looking at what I''m choosing to call two-deep association traversals. > > I have obviously got the option to simply use Ruby to do this, but > I''d rather use find, obviously, because it''ll generate proper SQL > and I''d say it''d be much faster. > > My quesiton is basically "Is there a way to do this elegantly using > the find command and some option I''m not aware of as yet, or do I > have to resort to using Ruby to do my pagination and limiting?". > > I''d like to be able to do stuff like InventoryItem.find > (:all,''brand.name LIKE "%nike%") or somesuch. > > Thanks in advance, > Julian.