On 6/21/05, Lindsay <hellolindsay-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> In my app, Users want to buy Things.
> I have three models:
>
> User : Name, Location
> Thing : Name, Description
> & Wants_To_Buy : User.id, Thing.id
>
> I want to select all the Users who want to buy at least one Thing. Is
> there any way of doing this without writing my own SQL query?
Sure, I''d recommend you think about renaming the model, as
it''s a
little confusing.
Assuming it''s called WishlistItem
class WishlistItem < ActiveRecord::Base
belongs_to :user
belongs_to :thing
end
You could then write:
@thing.wishlist_items.collect {|wtb| wtb.user}
But I''d suggest you stick a method in Thing,
class Thing < ActiveRecord::Base
#...
def interested_users
wishlist_items.collect {|wi| wi.user }
end
end
--
Cheers
Koz