- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直在阅读有关 inverse_of 的文章,而我在网上看到的所有内容似乎都不一致并使我感到困惑。如果你看 here ,你可以看到
There are a few limitations to inverse_of support:
- They do not work with :through associations.
- They do not work with :polymorphic associations.
- They do not work with :as associations.
- For belongs_to associations, has_many inverse associations are ignored.
class Customer < ActiveRecord::Base
has_many :orders, :inverse_of => :customer
end
class Order < ActiveRecord::Base
belongs_to :customer, :inverse_of => :orders
end
If you are using a belongs_to on the join model, it is a good idea to set the :inverse_of >option on the belongs_to, which will mean that the following example works correctly where >tags is a has_many :through association):
@post = Post.first
@tag = @post.tags.build :name => "ruby"
@tag.save
The last line ought to save the through record (a Taggable). This will only work if the >:inverse_of is set:
class Taggable < ActiveRecord::Base
belongs_to :post
belongs_to :tag, :inverse_of => :taggings
end
最佳答案
总是指定它没有什么坏处,它将允许 rails 优化对象的加载,这样您就可以在两个方向上上下事件记录模型关系链,而不会出现奇怪的错误,即更改一个对象上的值不会改变它的引用.
将它设置为不起作用的唯一问题是您之前所说的某些类型,如果它们无声地失败,如果您在更改某些内容后在方法链中走错了路,您将获得上述错误
这是来自 rails API
d = Dungeon.first
t = d.traps.first
d.level == t.dungeon.level # => true
d.level = 10
d.level == t.dungeon.level # => false
关于ruby-on-rails - 使用 ActiveRecord 在 rails 3 中 inverse_of 的限制是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16076823/
当我使用 has_one 时,它工作得很好,但在 has_many 上却不行。在这里您可以看到 object_id 不同,因为它运行了另一个 SQL 来再次获取它。 ruby-1.9.2-p290
如这里所述: http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html inverse_of 似乎告
来自 Rails Guide : There are a few limitations to inverse_of support: They do not work with :through a
根据documentation for ActiveRecord::Associations::ClassMethods , ActiveRecord 可以自动判断一个 belongs_to一个模块的
我有 2 个这样的模型: class User :user validates_length_of :user_services, :maximum => 3 end class UserS
inverse_of 似乎在 after_initialize 回调中不起作用 class User :user end class Face :faces after_initialize
什么 inverse_of在 mongoid 协会中是什么意思?我可以通过使用它而不是没有它的关联来获得什么? 最佳答案 在简单的关系中,两个模型只能以一种方式关联,关系的名称自动为其关联的模型的名称
我正在尝试理解inverse_of,但我不明白。 生成的 sql 是什么样子的(如果有的话)? 如果与 :has_many、:belongs_to 和 :has_many_and_belongs_to
我是第一次使用 Rails 4 开发 Web 应用程序。我正在使我所有的模型关联成为双向的,并在任何允许的地方使用 inverse_of。 通过阅读文档,我形成了这样的印象,即这可能是最佳实践,但从来
我有以下设置 class Player :player do def in_hand find_all_by_location('hand') end end end
我有这样一个模型: class Thing < ApplicationRecord belongs_to :thing has_many :things end 在 rails 5 中这是有效
我已经在这个问题上撞了一段时间了。同时保存新推荐(父对象)和约会(子对象)时会出现问题。我对其他嵌套对象做了类似的处理,但似乎无法使其与单表继承——约会表一起工作。出于某种原因,inverse_of
在文档中它说你可以使用 inverse_of: nil 但并没有真正描述用例: http://mongoid.org/en/mongoid/docs/relations.html#has_and_be
我无法在创建时让inverse_of在连接模型上工作。我不确定这是一个错误还是只是没有实现。我有以下模型: class Challenge :group_challenges has_many
该文档暗示 inverse_of 将适用于除多态关联之外的所有内容。 http://api.rubyonrails.org/classes/ActiveRecord/Associations/Clas
我在不同的地方都读过它在某些情况下会自动推断出来,但是我发现关于此功能的文档很难理解。谁能阐明一些原则? 特别是Rails何时可以推断,Rails何时不能推断逆? 我想考虑 多态关联 单表继承(例如B
我有两个具有可选关系的模型 has_many - belongs_to,如下所示: class Journey < ApplicationRecord has_many :activities,
我一直在阅读有关 inverse_of 的文章,而我在网上看到的所有内容似乎都不一致并使我感到困惑。如果你看 here ,你可以看到 There are a few limitations to in
我有一个“has_many”关系的问题,我从 ActiveRecord 中得到一个随机异常: product = Product.create!(valid_attributes) product.p
嗯,标题是不言自明的。但是,让我详细说明一下。首先,我正在使用 Mongoid,这是一个用于在 Rails 应用程序中使用 MongoDB 的 Gem。 #1) 我有一个大集合,其中包含有关 map
我是一名优秀的程序员,十分优秀!