edw*_*dmp 5 ruby-on-rails has-and-belongs-to-many rails-activerecord ruby-on-rails-5
我有两个彼此具有 HABTM 关系的 ActiveRecord 模型。当我通过允许通过选中复选框添加区域的表单添加 AccessUnit 时,我收到一个异常,即 AccessUnitUpdaterJob 无法排队,因为传递的访问单元无法序列化(由于缺少标识符) 。当在主对象上手动调用保存时,问题得到解决,但当然这是一种解决方法,而不是正确的修复方法。
太长了;看来 after_add 回调是在保存主对象之前触发的。我实际上不确定这是否是 Rails 中的错误或预期行为。我正在使用 Rails 5。
我遇到的确切错误是:
ActiveJob::SerializationError in AccessUnitsController#create
Unable to serialize AccessUnit without an id. (Maybe you forgot to call save?)
Run Code Online (Sandbox Code Playgroud)
这是一些代码,以便您可以了解问题的上下文:
class AccessUnit < ApplicationRecord
has_and_belongs_to_many :zones, after_add: :schedule_access_unit_update_after_zone_added_or_removed, after_remove: :schedule_access_unit_update_after_zone_added_or_removed
def schedule_access_unit_update_after_zone_added_or_removed(zone)
# self.save adding this line solves it but isn't a proper solution
puts "Access unit #{name} added or removed to zone #{zone.name}"
# error is thrown on this line
AccessUnitUpdaterJob.perform_later self
end
end
class Zone < ApplicationRecord
has_and_belongs_to_many :access_units
end
Run Code Online (Sandbox Code Playgroud)
在我看来这不是一个错误。一切都按预期进行。在保存此图表之前,您可以创建复杂的对象图表。在此创建阶段,您可以将对象添加到关联中。这是您想要触发此回调的时间点,因为它说after_addand not after_save。
例如:
@post.tags.build name: "ruby" # <= now you add the objects
@post.tags.build name: "rails" # <= now you add the objects
@post.save! # <= now it is to late, for this callback, you added already multiple objects
Run Code Online (Sandbox Code Playgroud)
也许使用before_add回调更有意义:
class Post
has_many :tags, before_add: :check_state
def check_state(_tag)
if self.published?
raise CantAddFurthorTags, "Can't add tags to a published Post"
end
end
end
@post = Post.new
@post.tags.build name: "ruby"
@post.published = true
@post.tags.build name: "rails" # <= you wan't to fire the before_add callback now, to know that you can't add this new object
@post.save! # <= and not here, where you can't determine which object caused the error
Run Code Online (Sandbox Code Playgroud)
您可以在《The Rails 4 Way》一书中阅读一些有关这些回调的内容
对于你的情况,你必须重新思考你的逻辑。也许你可以使用after_save回调。我的 2 美分:您考虑从回调切换到服务对象。回调并不是没有代价的。它们并不总是易于调试和测试。
| 归档时间: |
|
| 查看次数: |
1792 次 |
| 最近记录: |