Reputation: 183
Having the following models:
class Card < ActiveRecord:Base
# ...whatever
belongs_to :model
end
class Model < ActiveRecord:Base
# attribute: rank
has_many :cards
end
Considering the following code:
mod = Model.new
card = Card.first
an_attribute = "rank"
another_attribute = "cards"
mod.send("#{an_attribute}=", 1) # this works fine, OK
mod.cards << card # this works fine as well, OK
mod.send("#{another_attribute}<<", card) # this does not work, hence my question
The last line of code returns an error undefined method 'cards<<'
So my question is: how can I append the card
element to mod.cards
while accessing the cards
has_many
relation via a variable, i.e. by storing the name of the association in a variable ?
Upvotes: 0
Views: 301