Is it possible to iterate through accessors in a model?
I have a model that looks like:
class Account < User
#Required fields
attr_accessor :email, :first_name, :last_name, :organizations
end
I get a JSON back that looks like
@name : email
@value : nick@nick.com
@name : first_name
@value : nick
I have a bunch of code that looks like:
unless(item.Field["@name"].to_s.match('E-mail').nil?)
@account_new.email = item.Field["@value"].to_s
end
unless(item.Field["@name"].to_s.match('First Name').nil?)
@account_new.first_name = item.Field["@value"].to_s
end
Which is terrible code. What I’m wondering is it possible to iterate through the accessors in my model and get the name of the accessor and use that to pattern match in the JSON object and then do an assignment?
I tried using .instance_variables.each do |variable|
But it doesn’t seem to work the way I thought it should. Is there a better way to do this?
In Ruby, an attribute can be set by sending a message: