ruby on rails - Add record to a has_and_belongs_to_many relationship -


i have 2 models, users , promotions. idea promotion can have many users, , user can have many promotions.

class user < activerecord::base   has_and_belongs_to_many :promotions end  class promotion < activerecord::base   has_and_belongs_to_many :users end 

i have promotions_users table/model, no id of own. references user_id , promotions_id

class promotionsusers < activerecord::base end 

so, how add user promotion? i've tried this:

user = user.find(params[:id]) promotion = promotion.find(params[:promo_id]) promo = user.promotions.new(promo) 

this results in following error:

nomethoderror: undefined method `stringify_keys!' #<promotion:0x10514d420> 

if try line instead: promo= user.promotions.new(promo.id)

i error:

typeerror: can't dup fixnum 

i'm sure there easy solution problem, , i'm not searching solution right way.

user = user.find(params[:id]) promotion = promotion.find(params[:promo_id]) user.promotions << promotion 

user.promotions array of promotions tied user.

see apidock different functions have available.


Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -