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
Post a Comment