【发布时间】:2012-04-14 12:10:46
【问题描述】:
我遇到了 datamapper 不更新模型的问题。我可以毫无问题地创建和保存模型。我启用了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
这是在控制器中更新的地方:
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
当控制器被调用时update总是返回true,并且错误列表中永远不会有错误。设置为:debug 的数据映射器日志仅显示用于检索user 和user_profile 的SELECT 查询,仅此而已。为什么我可以save 一个新创建的模型,但不允许update 相同的模型?
【问题讨论】:
标签: ruby datamapper