【问题标题】:ActiveRecord has_many through inverse_of causing rails admin to work?ActiveRecord has_many 通过 inverse_of 导致 rails admin 工作?
【发布时间】:2014-10-31 02:00:10
【问题描述】:

我知道在 ActiveRecord 中的 has_many :through 关联不适用于 inverse_of,但是我在 Rails Admin gem 中遇到了我的 has many through 模型的问题,当我尝试编辑或创建新资源时,加载该页面导致模板渲染的无限递归。

问题是here

当我尝试删除时

accepts_nested_attributes_for :recipes, allow_destroy: true

成功了,错误消失了。

但当我保留 Accept_nested_attributes_for 并放置时它也有效

has_many :recipes, :through => :recipe_categorizations, :inverse_of => :categories

:inverse_of 结尾。

这怎么可能? inverse_of 对 :through 不可用吗?它的存在会造成任何伤害还是被忽略?

如果有帮助,我正在使用 Rails 4。

【问题讨论】:

    标签: ruby-on-rails ruby activerecord associations rails-admin


    【解决方案1】:

    如果您不设置:inverse_of 记录,关联将尽最大努力将自己与正确的逆相匹配。自动逆向检测仅适用于 has_manyhas_onebelongs_to 关联。

    AssociationReflection::INVALID_AUTOMATIC_INVERSE_OPTIONS 常量中定义的关联的额外选项也将防止自动找到关联的逆。

    反向关联的自动猜测使用基于类名的启发式算法,因此它可能不适用于所有关联,尤其是具有非标准名称的关联。

    这里是 source 用于设置逆数。

    【讨论】:

    • 但是,如果它不适用于 :through,为什么它会导致 Rails 管理员正确识别逆呢?出于 Rails 管理员的目的,保留它有什么害处吗?
    猜你喜欢
    • 2013-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-17
    • 1970-01-01
    相关资源
    最近更新 更多