I have a Ruby hash that I'm retrieving via a remote web API. I have an ActiveRecord model that has the same attributes as the keys in the hash. Is there a trivial way with Ruby on Rails 4 to assign the key/val pairs from the hash to the model instance? Is it possible to ignore the keys that do not exist?
我有一个Ruby哈希,我正在通过远程Web API检索。我有一个ActiveRecord模型,它具有与哈希中的键相同的属性。 Ruby on Rails 4是否有一种简单的方法可以将哈希中的键/值对分配给模型实例?是否可以忽略不存在的密钥?
2 个解决方案
#1
3
Super-easy!
Update the attributes without saving:
更新属性而不保存:
model.attributes = your_hash
# in spite of resembling an assignemnt, it just sets the given attributes
Update attributes saving:
更新属性保存:
model.update_attributes(your_hash)
# if it fails because of validation, the attributes are update in your object
# but not in the database
Update attributes, save, and raise if unable to save
如果无法保存,则更新属性,保存和提升
model.update_attributes!(your_hash)
#2
1
According to the Rails docs:
根据Rails文档:
update(attributes)
Updates the attributes of the model from the passed-in hash and saves the record, all wrapped in a transaction. If the object is invalid, the saving will fail and false will be returned.
从传入的哈希更新模型的属性并保存记录,所有记录都包含在事务中。如果对象无效,则保存将失败并返回false。
So try
model.update(dat_hash) #dat_hash being the hash with the attributes
model.update(dat_hash)#dat_hash是属性的哈希
I have been doing the same thing in Rails 3.2 using update_attributes, which is the same thing. Here is my code:
我一直在使用update_attributes在Rails 3.2中做同样的事情,这是同样的事情。这是我的代码:
def update
@form = get_form(params[:id])
@form.update_attributes(params[:form])
@form.save
if @form.save
render json: @form
else
render json: @form.errors.full_messages, status: :unprocessable_entity
end
end
It only updates the attributes that are in the hash.
它仅更新散列中的属性。
#1
3
Super-easy!
Update the attributes without saving:
更新属性而不保存:
model.attributes = your_hash
# in spite of resembling an assignemnt, it just sets the given attributes
Update attributes saving:
更新属性保存:
model.update_attributes(your_hash)
# if it fails because of validation, the attributes are update in your object
# but not in the database
Update attributes, save, and raise if unable to save
如果无法保存,则更新属性,保存和提升
model.update_attributes!(your_hash)
#2
1
According to the Rails docs:
根据Rails文档:
update(attributes)
Updates the attributes of the model from the passed-in hash and saves the record, all wrapped in a transaction. If the object is invalid, the saving will fail and false will be returned.
从传入的哈希更新模型的属性并保存记录,所有记录都包含在事务中。如果对象无效,则保存将失败并返回false。
So try
model.update(dat_hash) #dat_hash being the hash with the attributes
model.update(dat_hash)#dat_hash是属性的哈希
I have been doing the same thing in Rails 3.2 using update_attributes, which is the same thing. Here is my code:
我一直在使用update_attributes在Rails 3.2中做同样的事情,这是同样的事情。这是我的代码:
def update
@form = get_form(params[:id])
@form.update_attributes(params[:form])
@form.save
if @form.save
render json: @form
else
render json: @form.errors.full_messages, status: :unprocessable_entity
end
end
It only updates the attributes that are in the hash.
它仅更新散列中的属性。