0
是否可以遍歷模型中的訪問器?如何迭代模型中的訪問器?
我有一個模型,看起來像:
class Account < User
#Required fields
attr_accessor :email, :first_name, :last_name, :organizations
end
我得到一個JSON回來,看起來像
@name : email
@value : [email protected]
@name : first_name
@value : nick
我有一大堆的代碼看起來像:
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
這是可怕的代碼。我想知道是否可以遍歷模型中的訪問器並獲取訪問者的名稱,並使用它在JSON對象中模式匹配,然後執行任務?
我試過使用.instance_variables.each do | variable |
但它似乎沒有按照我認爲應該的方式工作。有一個更好的方法嗎?
謝謝!正是我在尋找的,再次感謝。 – user1357182
不客氣:) – ccyrille