Add element to rails has_many relation "dynamically" -


having following models:

class card < activerecord:base   # ...whatever   belongs_to :model end  class model < activerecord:base   # attribute: rank   has_many  :cards end 

considering following code:

  mod = model.new   card = card.first   an_attribute = "rank"   another_attribute = "cards"    mod.send("#{an_attribute}=", 1) # works fine, ok   mod.cards << card      # works fine well, ok   mod.send("#{another_attribute}<<", card) # not work, hence question 

the last line of code returns error undefined method 'cards<<'

so question is: how can append card element mod.cards while accessing cards has_many relation via variable, i.e. storing name of association in variable ?

mod.send("#{another_attribute}").push(card) 

Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

[C++][SFML 2.2] Strange Performance Issues - Moving Mouse Lowers CPU Usage -

ios - Possible to get UIButton sizeThatFits to work? -