Forum: Ruby on Rails An Association Problem: AR::Base.find{:include} and n-deep a

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Julian L. (Guest)
on 2006-03-15 14:45
(Received via mailing list)
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 L. (Guest)
on 2006-03-17 03:37
(Received via mailing list)
So I guess no one has a response to this?
This topic is locked and can not be replied to.