ruby Rails 3.0.x has_many 仅在创建时出现异常,其他操作正常

发布于 2024-12-25 03:48:21 字数 1188 浏览 1 评论 0原文

以下模型适用于读取、更新和删除,但不适用于创建。

class Space < ActiveRecord::Base
  has_many :space_prices, :dependent => :destroy
  accepts_nested_attributes_for :space_prices, :allow_destroy => true
  has_many :space_price_availabilities, through: :space_prices, :order => "day_of_week, begin_time", read_only: true
end

class SpacePrice < ActiveRecord::Base
  belongs_to :space
  has_many :space_price_availabilities, inverse_of: :space_price, :dependent => :destroy, :order => "day_of_week, begin_time"
  accepts_nested_attributes_for :space_price_availabilities, :allow_destroy => true
end

class SpacePriceAvailability < ActiveRecord::Base
  belongs_to :space_price
end

我知道解决方法是实现 Space.space_price_availities 方法。仅仅总是引用 Space.space_prices.space_price_availities 是不够的,b/c 我需要对完整列表进行扁平化和排序。

如果我注释掉 Space 中的 has_many :through ,我可以很好地创建新的空间。然后,如果我在 Space 中取消注释 has_many :through ,我可以很好地读取它们并更新它们。只有在创建时我才会收到以下异常。

ActiveRecord::HasManyThroughCantAssociateThroughHasOneOrManyReflection

检查日志,正在生成并调用正确的 SQL,但随后遇到此异常并将其回滚。

有什么想法吗?我真的很想知道我是否遗漏了有关 Rails 协会的一些基本知识。对我来说,它看起来非常简单。该模型与 API 文档中的示例基本相同。

The following model works fine for reads, updates, and deletes but not for creating.

class Space < ActiveRecord::Base
  has_many :space_prices, :dependent => :destroy
  accepts_nested_attributes_for :space_prices, :allow_destroy => true
  has_many :space_price_availabilities, through: :space_prices, :order => "day_of_week, begin_time", read_only: true
end

class SpacePrice < ActiveRecord::Base
  belongs_to :space
  has_many :space_price_availabilities, inverse_of: :space_price, :dependent => :destroy, :order => "day_of_week, begin_time"
  accepts_nested_attributes_for :space_price_availabilities, :allow_destroy => true
end

class SpacePriceAvailability < ActiveRecord::Base
  belongs_to :space_price
end

I know the workaround is to implement a Space.space_price_availabilities method. It is not sufficient to just always reference Space.space_prices.space_price_availabilities b/c I need the complete list flattened and sorted.

If I comment out the has_many :through in Space, I can create new Spaces fine. Then if I uncomment the has_many :through in Space, I can read them and update them fine. It's only on create that I get the following exception.

ActiveRecord::HasManyThroughCantAssociateThroughHasOneOrManyReflection

Checking the logs, the right SQL is being generated and called, but then hits this exception and rolls it back.

Any ideas? I'd really like to know if I'm missing something fundamental about the rails associations. It looks pretty straight-forward to me. The model is essentially the same as the example in the API documentation.

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

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

发布评论

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

评论(1

别靠近我心 2025-01-01 03:48:21

添加 :autosave =>假

has_many :space_price_availabilities, autosave: false, 
  inverse_of: :space_price, :dependent => :destroy, 
  :order => "day_of_week, begin_time", autosave: false

Add :autosave => false on

has_many :space_price_availabilities, autosave: false, 
  inverse_of: :space_price, :dependent => :destroy, 
  :order => "day_of_week, begin_time", autosave: false
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文