DataMapper 保存失败但没有错误

发布于 2024-10-10 01:26:41 字数 1099 浏览 2 评论 0原文

当我尝试使用 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 更具体地告诉我出了什么问题?

When I try to modify and then save a model using DataMapper I get a SaveFailure exception but no errors.

Specifically I see this message:
"MonthlyBill#save returned false, MonthlyBill was not saved"

This is the code doing the saving:

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

The User model:

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

The MonthlyBill model:

class MonthlyBill
  include DataMapper::Resource

  property :id,             Serial
  property :description,    String
  property :expected_amount,Decimal
  property :pay_period,     Integer

  belongs_to :user
end

What is the issue and, more importantly, how can I get DataMapper to tell me more specifically what is wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柒夜笙歌凉 2024-10-17 01:26:41

嗯 - 那些资本化的财产对我来说看起来令人担忧。我会做...

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

Hmm - those capitalised properties look worrying to me. I would do...

has n, :monthly_bills 
has 1, :current_pay_period #do you really have a CurrentPayPeriod model?!

And then try:

monthly_bill = MonthlyBill.new(:description=>description,:expected_amount=>expected_amount, :pay_period=>pay_period, :user=>user)
monthly_bill.save
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文