ruby - find_or_create_by on intermediate table in rails 2.3 -
i have 2 tables , related third table follows.
class < activerecord::base has_many :b, :through => :ab has_many :ab end class b < activerecord::base has_many :a, :through => :ab has_many :ab end class ab < activerecord::base belongs_to :b, belongs_to :a end
i want able insert intermediate table(ab), using find_or_create.but find_or_create doesnot work on ab. how can it, other finding , creating seperately ??
ps: dont want use has_and_belongs_to_many because doesnot create intermediate model , want insert intermediate table without creating rows in end tables, , b.
some suggestions please??
find_or_create_by
has been added in rails 4. can write (should go initializers):
class activerecord::base def self.find_by(params) scoped.where(params).limit(1).first end def self.find_or_create_by(params) find_by(params) || create(params) end end
Comments
Post a Comment