Jac*_*own 9 ruby activerecord ruby-on-rails has-many-through ruby-on-rails-3
我有三个相关的模型,如下所示:
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
Run Code Online (Sandbox Code Playgroud)
当我做的事情:
doc = user.documents.build
doc.update_attributes(:product_ids => [1,2])
Run Code Online (Sandbox Code Playgroud)
而description
验证失败,然后我得到false
,并在适当的错误doc
.这正是我想要的.
但是,如果doc
已经存在,例如:
doc = user.documents.first
doc.update_attributes(:product_ids => [1,2])
Run Code Online (Sandbox Code Playgroud)
而description
验证失败,然后我得到一个ActiveRecord::RecordInvalid
错误.
我确切地知道为什么会发生这种情况 - insert_record
来自has_many_through_association.rb的方法在save!
内部调用,传播错误.它会提前退出,跳过此调用,以获取新记录.
有什么方法可以设置我的模型来防止这种情况save!
吗?还是我被迫rescue
出错?
编辑
我已经尝试了下面Carlos Drew描述的设置; 我也试着设置validates_associated :descriptions
,并添加inverse_of: :whatever
到has_many :descriptions
选项哈希值.我也尝试before_validation
在Product
和Document
模型上设置回调,但显然关联回调首先运行(?).每次尝试似乎都会产生完全相同的错误消息.
我正在从下面的控制台粘贴我的错误跟踪.
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>'
Run Code Online (Sandbox Code Playgroud)
我的直觉是,您过度设计了模型的验证before_add: :validate_description
。您没有使用标准 Rails/ActiveRecord 方法和约定吗?具体来说,validates: true
可以设置关联模型之间的验证处理。
尽管如此,关联验证仍然存在一些问题,我建议阅读以下内容:
编辑
我对此非常好奇,并按照您所描述的那样,通过规范(并且它位于公共 github 项目中)复制了该问题。我仍然认为手动 before_add 验证设计过度,我没有使用它们,但我遇到了您描述的问题。
所以,我想了解的是你所遇到的情况是否是预期和想要的。如果不是固执己见的话,Rails 就什么都不是,也许使用直接设置 has_many-through 关联是一种编码器注意的用例。需要明确的是,您正在做的事情有点奇怪:当您要求设置时document.product_ids
,您实际上正在做的是在某些描述对象上设置匹配的 document_id 和 Product_id 。正确的?这很奇怪,而且意图/预期结果非常不清楚。
那么替代方法是什么?您正在做的是将描述添加到文档中,这些描述是关于产品的。那么为什么不通过描述接口与文档产品进行交互呢?我认为,这应该避免 has_many-through setter 的怪异,并提供一个更清晰的界面。
归档时间: |
|
查看次数: |
1741 次 |
最近记录: |