ruby-on-rails - 通过关联获取 has_many 的 ActiveRecord::RecordInvalid 错误;连接表上的验证问题

标签 ruby-on-rails ruby ruby-on-rails-3 activerecord has-many-through

我有如下三个关联模型:

class Product < ActiveRecord::Base
  belongs_to :user
  has_many :descriptions, {
    dependent: :destroy,
    before_add: [:add_user_id_to_description, :validate_description]
  }
  has_many :documents, through: :descriptions

  # ...

  def validate_description(d)
    unless d.valid?
      d.errors[:user_id].each do |err|
        self.errors.add(:base, "Doc error: #{err}")
      end
    end
  end
end

class Document < ActiveRecord::Base
  belongs_to :user
  has_many :descriptions, {
    dependent: :destroy,
    before_add: [:add_user_id_to_description, :validate_description]
  }
  has_many :products, through: :descriptions
end

class Description < ActiveRecord::Base
  belongs_to :user
  belongs_to :product
  belongs_to :document
end

当我做类似的事情时:

doc = user.documents.build
doc.update_attributes(:product_ids => [1,2])

并且 description 验证失败,然后我得到 falsedoc 上的相应错误。这正是我想要的。

但是,如果 doc 已经存在,例如:

doc = user.documents.first
doc.update_attributes(:product_ids => [1,2])

并且 description 验证失败,然后我得到一个 ActiveRecord::RecordInvalid 错误。

我很清楚为什么会这样——insert_record 方法来自 has_many_through_association.rb在内部调用 save!,它会传播错误。它提前退出,跳过此调用,以获取新记录。

有什么方法可以设置我的模型来防止这种保存!?还是我被迫从错误中拯救

编辑

我已经尝试了下面 Carlos Drew 描述的设置;我还尝试设置 validates_associated :descriptions,并将 inverse_of::whatever 添加到 has_many :descriptions 选项哈希。我还尝试在 ProductDocument 模型上设置一个 before_validation 回调,但显然关联回调首先运行(?)。每次尝试似乎都会产生完全相同的错误消息。

我正在从下面的控制台粘贴我的错误跟踪。

Document Load (1.8ms)  SELECT "documents".* FROM "documents" WHERE "documents"."user_id" = 19 ORDER BY "documents"."id" DESC LIMIT 1
   (1.0ms)  BEGIN
  Product Load (41.7ms)  SELECT "products".* FROM "products" WHERE "products"."id" = $1 LIMIT 1  [["id", 3640]]
  Product Load (4.1ms)  SELECT "products".* FROM "products" INNER JOIN "descriptions" ON "products"."id" = "descriptions"."product_id" WHERE "descriptions"."document_id" = 3552
  User Load (7.0ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 19 LIMIT 1
  Account Load (2.0ms)  SELECT "accounts".* FROM "accounts" WHERE "accounts"."user_id" = 19 LIMIT 1
   (0.9ms)  SELECT COUNT(*) FROM "descriptions" WHERE "descriptions"."user_id" = 19
   (1.2ms)  ROLLBACK
ActiveRecord::RecordInvalid: Validation failed: User You have reached limit of 1
    from /usr/local/rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.13/lib/active_record/validations.rb:56:in `save!'
    from /usr/local/rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.13/lib/active_record/attribute_methods/dirty.rb:33:in `save!'
    from /usr/local/rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.13/lib/active_record/transactions.rb:264:in `block in save!'
    from /usr/local/rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.13/lib/active_record/transactions.rb:313:in `block in with_transaction_returning_status'
    from /usr/local/rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'
    from /usr/local/rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.13/lib/active_record/transactions.rb:208:in `transaction'
    from /usr/local/rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.13/lib/active_record/transactions.rb:311:in `with_transaction_returning_status'
    from /usr/local/rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.13/lib/active_record/transactions.rb:264:in `save!'
    from /usr/local/rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.13/lib/active_record/associations/has_many_through_association.rb:85:in `save_through_record'
    from /usr/local/rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.13/lib/active_record/associations/has_many_through_association.rb:52:in `insert_record'
    from /usr/local/rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.13/lib/active_record/associations/collection_association.rb:496:in `block (2 levels) in concat_records'
    from /usr/local/rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.13/lib/active_record/associations/collection_association.rb:344:in `add_to_target'
    from /usr/local/rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.13/lib/active_record/associations/collection_association.rb:495:in `block in concat_records'
    from /usr/local/rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.13/lib/active_record/associations/collection_association.rb:493:in `each'
    from /usr/local/rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.13/lib/active_record/associations/collection_association.rb:493:in `concat_records'
    from /usr/local/rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.13/lib/active_record/associations/collection_association.rb:134:in `block in concat'
... 14 levels...
    from /usr/local/rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.13/lib/active_record/associations/builder/collection_association.rb:71:in `block in define_writers'
    from /usr/local/rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.13/lib/active_record/attribute_assignment.rb:85:in `block in assign_attributes'
    from /usr/local/rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.13/lib/active_record/attribute_assignment.rb:78:in `each'
    from /usr/local/rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.13/lib/active_record/attribute_assignment.rb:78:in `assign_attributes'
    from /usr/local/rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.13/lib/active_record/persistence.rb:216:in `block in update_attributes'
    from /usr/local/rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.13/lib/active_record/transactions.rb:313:in `block in with_transaction_returning_status'
    from /usr/local/rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'
    from /usr/local/rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.13/lib/active_record/transactions.rb:208:in `transaction'
    from /usr/local/rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.13/lib/active_record/transactions.rb:311:in `with_transaction_returning_status'
    from /usr/local/rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.13/lib/active_record/persistence.rb:215:in `update_attributes'
    from (irb):2
    from /usr/local/rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.13/lib/rails/commands/console.rb:47:in `start'
    from /usr/local/rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.13/lib/rails/commands/console.rb:8:in `start'
    from /usr/local/rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.13/lib/rails/commands.rb:41:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'

最佳答案

我的直觉是您过度设计了带有 before_add::validate_description 的模型验证。您没有使用标准的 Rails/ActiveRecord 方法和约定吗?具体来说,validates: true可以设置关联模型之间的验证处理。

关于关联验证仍然存在一些陷阱,我建议阅读以下内容:

编辑

我对此非常好奇,并按照您的描述通过规范(在 public github project 中)复制了该问题。我仍然认为手动 before_add 验证设计过度,我没有使用它们,但我遇到了您描述的问题。

所以,我想了解的是,您遇到的情况是否符合预期和期望。如果不自以为是,Rails 就什么都不是,也许使用 has_many-through 关联的直接设置是一种编码人员注意用例。明确地说,您正在做的事情有点奇怪:当您要求设置 document.product_ids 时,您实际上正在做的是在某些描述对象上设置匹配的 document_id 和 product_id。正确的?这很奇怪,而且意图/预期结果非常不清楚。

那么有什么替代方法呢?您正在做的是向文档添​​加描述,而这些描述是关于产品的。那么,为什么不通过描述界面与文档产品进行交互呢?我认为,这应该避免 has_many-through setter 怪异,并提供更清晰的界面。

关于ruby-on-rails - 通过关联获取 has_many 的 ActiveRecord::RecordInvalid 错误;连接表上的验证问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20599679/

相关文章:

ruby-on-rails - 使用 rails 服务器启动服务器时出错,sqlite 错误

ruby-on-rails - rails : random unique array of fixed length

ruby-on-rails - 如何为 Rails 控制台安装 "readline"

ruby - 是否存在可以在 Ruby 中生成 Markdown 的 Markdown 解析器?

sql - 有没有更有效的方法来使用 ActiveRecord 对此进行编码?

ruby-on-rails-3 - 在链接(符号链接(symbolic link))文件之前要求 Capistrano 运行任务

ruby-on-rails - Heroku Dyno 上的总内存上升

ruby-on-rails - RSpec 找不到我的 Controller 未初始化的常量

ruby-on-rails - 使用 ruby​​ 进行系统测试(可能是 rspec)

ruby-on-rails - 如何更新 Ruby 中的引用列