below question is about selecting the attributes enabled by "attr_accessor" for an object. Example:
class Openhour < ActiveRecord::Base
belongs_to :shop
attr_accessor :monday, :tuesday, :wednesday, :thursday, :friday, :saturday, :sunday
end
This allows me to
week = Openhour.new
week.monday = "Open"
week.tuesday = "Closed"
My question: How can I select the attr_accessors by using a variable from a loop? In below case I would use dayname
to select the attr_accessor
.
@schedules.each do |schedule|
%w(monday tuesday wednesday thursday friday saturday sunday).each_with_index do |dayname,dayname_index|
week.dayname = schedule.day == dayname_index ? "Open" : "Closed"
end
end
This, however, would result in
*** NoMethodError Exception: undefined method `dayname' for #<Model>
Thanks in advance!
you can use
Or you can work with it like
instance_variables