我有三个模型:
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)