Ruby(2)和Rails(3.2)是否可以在单个命令中更新选择的字段。例如,我需要更新钩子中的一组字段,其中所有名称都匹配。我最终得到了类似的东西
def set_address
assign_attributes(address1: subscription.subscriber.address1,
address2: subscription.subscriber.address2,
address3: subscription.subscriber.address3,
city: subscription.subscriber.city,
county: subscription.subscriber.county,
country_id: subscription.subscriber.country_id,
postcode: subscription.subscriber.postcode)
end
鉴于所有字段名称在两个模型中匹配,有更简单的方法,或者将字段组合在一起以便我可以执行
def set_address
address = subscription.subscriber.address
end
任何建议表示赞赏
答案 0 :(得分:0)
也许是这样的:
[:address1, :address2, :address3, :city, :country, :country_id, :postcode].each do |field|
send("#{field}=", subscription.subscriber.send(field))
end