我在数据映射器未更新模型时遇到问题。我可以毫无问题地创建和保存模型。我启用了 raise_on_save_failure
并检查了 update
的返回值,但没有看到任何错误。
这是模型:
class UserProfile
include DataMapper::Resource
attr_accessor :id, :wants_hints, :is_beta_user
property :id, Serial #auto-increment integer key
property :is_beta_user, Boolean
property :wants_hints, Boolean
has 1, :user, :through => Resource
end
这是它在 Controller 中更新的地方:
if user = User.get(request.session[:user])
if request.params[:user_profile]
beta = request.params[:user_profile].has_key?('is_beta_user')
hints = request.params[:user_profile].has_key?('wants_hints')
user.user_profile.update({:is_beta_user => beta, :wants_hints => hints}) # returns true
Log.puts user.user_profile.errors.each {|e| Log.puts e.to_s} # returns empty list []
end
end
当调用 Controller update
时总是返回true,并且错误列表中永远不会有错误。设置为 :debug
的数据映射器日志仅显示用于检索 user
和 user_profile
的 SELECT
查询仅此而已。为什么我能够保存
一个新创建的模型,但不允许更新
相同的模型?
最佳答案
删除 attr_accessor
解决了这个问题。根据我的研究,attr_accessor
用于不在数据库中的属性。
关于ruby - Datamapper "Update"不更新记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10107109/