当我尝试使用DataMapper修改然后保存模型时,我得到一个SaveFailure异常但没有错误。
具体来说,我看到这条消息: “MonthlyBill#save返回false,MonthlyBill未保存”
这是执行保存的代码:
post '/monthly_bills' do
with_authenticated_user do |user|
description = params[:description]
expected_amount = params[:expected_amount]
pay_period = params[:pay_period]
monthly_bill = MonthlyBill.new(:description=>description, :expected_amount=>expected_amount, :pay_period=>pay_period)
user.monthly_bills << monthly_bill
user.save
end
用户模型:
class User
include DataMapper::Resource
property :id, Serial
property :email_address, String
property :password, String
has n, :monthly_bills
has 1, :current_pay_period
end
MonthlyBill模型:
class MonthlyBill
include DataMapper::Resource
property :id, Serial
property :description, String
property :expected_amount,Decimal
property :pay_period, Integer
belongs_to :user
end
问题是什么,更重要的是,我如何让DataMapper更具体地告诉我哪些是错的?
答案 0 :(得分:1)
has n, :monthly_bills
has 1, :current_pay_period #do you really have a CurrentPayPeriod model?!
然后尝试:
monthly_bill = MonthlyBill.new(:description=>description,:expected_amount=>expected_amount, :pay_period=>pay_period, :user=>user)
monthly_bill.save