ActiveScaffold :: ReverseAssociationRequired创建新记录时出错

时间:2012-02-01 13:48:59

标签: ruby-on-rails ruby-on-rails-3 associations activescaffold

我有三种模式:

class LevelTwoArea < ActiveRecord::Base
  has_many :places, dependent: :restrict
end

class Place < ActiveRecord::Base
  belongs_to :level_two_area
  has_many   :producers, dependent: :restrict

  validates_presence_of :level_two_area_id
end

class Producer < ActiveRecord::Base
  belongs_to :place

  validates_presence_of :place_id
end

我还有一个控制器,每个型号都有一个ActiveScaffold。

问题是,当我想创建新的Place时,脚手架会向日志发出错误:

ActiveScaffold::ReverseAssociationRequired (Association producers: In order 
to support :has_one and :has_many where the parent record is new and the child 
record(s) validate the presence of the parent, ActiveScaffold requires the 
reverse association (the belongs_to).)

我不明白为什么......

请注意:

  • 其他类似的脚手架可以正常工作
  • 我可以在控制台中创建记录并使用关联:

    >> Place.new name: 'PONVOGO', level_two_area_id: 10144
    => #<Place id: nil, name: "PONVOGO", latitude: nil, longitude: nil, producers_count: nil, level_two_area_id: 10144, created_at: nil, updated_at: nil, marker: nil>
    >> _.save
    => true
    >> Producer.first.place.producers
    => [#<Producer id: 1, name: "KOFFI YAO GREGOIRE", place_id: 43, created_at: nil, updated_at: nil>]
    
  • 当我删除has_many :producers时,问题就消失了,尽管关联宏看起来很正常

  • 如果我删除dependent: :restrict选项
  • ,问题就不会消失
  • 我的producers_census模型上有一个Place列,我怀疑这样做会搞砸,但是在进行大量重构之前需要确认。从脚手架中删除此列将无法解决问题。
places表上的

字段:

Column             |            Type             |                                  
--------------------------------------------------
 id                | integer                     | non NULL (PK)       
 name              | character varying(50)       | non NULL
 latitude          | double precision            | 
 longitude         | double precision            | 
 producers_census  | integer                     | 
 level_two_area_id | integer                     | non NULL (FK)
 created_at        | timestamp without time zone | 
 updated_at        | timestamp without time zone | 
 marker            | geometry                    | 

我的整个PlacesController

class PlacesController < ApplicationController
  active_scaffold :place do |conf|
    conf.columns =  :name,
                    :level_two_area,
                    :latitude,
                    :longitude,
                    :producers_census
    export.columns.add  :id, 
                        :level_two_area_id

    [:latitude, :longitude].each {|column| conf.columns[column].options[:format] = "%.14f" }

    [               
      create,
      update,
      delete,
      batch_update 
    ].each do |action|
       action.link.security_method = :can_see_link?
    end
    batch_destroy.link.each {|sublink| sublink.security_method = :can_see_link? }

  end
end 

我在轨道上(3.0.5)/ active_scaffold_vho(3.0.17)/ ruby​​(1.9.2p180)

1 个答案:

答案 0 :(得分:3)

虽然之前我从未见过这个ActiveScaffold异常,但我认为这是正在发生的事情:

使用:inverse_of选项设置反向关联,并且在未保存记录时其效果仅可见。这就是您在控制台实验中看不到的原因。

试试这个:

place = Place.new
=> #<Place ...>
producer = place.producers.build
=> #<Producer ...>

producer.place
=> nil
place.producers.first.place
=> nil

完全在内存中构建时,这些记录将无法通过验证。如果你从不同时建立一个地方和一个制作人,那么你不必担心这一点。但是,如果你这样做,那么你的验证规则也应该写成检查是否存在:place(即对象),而不是:place_id

validates_presence_of :place

我不知道ActiveScaffold使用哪种构建/创建机制,但我猜想如果他们抛出此异常,他们可能会在内存中构建内容。

所以,要解决这个问题,请尝试:

class Producer < ActiveRecord::Base
  belongs_to :place, inverse_of: producers

  validates_presence_of :place
end

class Place < ActiveRecord::Base
  has_many :producers, inverse_of: place
end

注意:ActiveRecord过去常常不支持:inverse_of侧的has_many,而且该限制曾在源代码中进行过注释。从Rails 3.1开始,这似乎是固定的。如果您使用:inverse_of修改类,如图所示,那么使用内存中对象的控制台实验应该返回关联的对象。

您可能希望查看bi-directional associations

上的Rails API文档
相关问题