Raymond, I think you are mixing concerns here.
Concern #1 is finding the correct product by sku
Concern #2 is finding the inventory for a product (which, in this
case, you''ve identified by sku).
You could then have something along the lines of what Greg suggests:
@product = Product.find_by_sku some_sku
@inventory = @product.inventory
Note that you can also get the inventory based on sku directly using
includes and conditions properly:
class Inventory < ARec::Base
belongs_to :product
def self.find_by_sku(sku)
self.find(:all/
first, :include=>:product, :conditions=>[''products.sku =
?'', sku])
end
end
@inventory = Inventory.find_by_sku some_sku
I think you''ll be much better off in the long run using the ID field
for your associations. In this particular case sku will be come
redundant information kept in various places in your system and will
invariably lead to broken data when "the powers that be" decide to
implement a new strategy for assigning skus.
HTH,
AndyV
On May 13, 2:02 am, "Greg Donald"
<gdon...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> On Mon, May 12, 2008 at 11:18 PM, Raymond O''Connor
>
> <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:
> > I know there is a foreign_key attribute in associations, but
> > that doesn''t solve the whole issue since it ends up
comparing ID vs SKU,
> > not SKU vs SKU.
>
> Add a custom finder that searches by self.sku.
>
> def by_sku
> Inventory.find_by_sku( self.sku )
> end
>
> --
> Greg Donaldhttp://destiney.com/
--~--~---------~--~----~------------~-------~--~----~
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
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---