Wednesday, 15 August 2012

ruby on rails - has and _belongs_to_many lookup -



ruby on rails - has and _belongs_to_many lookup -

i know i'm gonna kick myself one...

i have trader model:

class trader < activerecord::base has_and_belongs_to_many :locations end

and location model:

class location < activerecord::base has_and_belongs_to_many :traders end

i have created class method called in_location:

def self.in_location(location) traders_found = [] traders = trader.all traders.each |trader| locations = trader.locations locations.each |trader_location| traders_found << trader if trader_location == location end end traders_found end

i have feeling kinds of wrong , rails can handle in 1 liner.

if query find of traders location of x

can't like:

location = location.find(1) # or whatever location.traders

?

seems don't need in_location method @ all.

ruby-on-rails

No comments:

Post a Comment