Ruby on rails ActiveScaffold::ReverseAssociationRequired创建新记录时出错
我有三种型号:Ruby on rails ActiveScaffold::ReverseAssociationRequired创建新记录时出错,ruby-on-rails,ruby-on-rails-3,associations,activescaffold,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_i
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
我不明白为什么
请注意:
- 其他类似的脚手架也能起作用
- 我可以在控制台中创建记录并使用关联:
我的整个>> 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>]
:位置控制器
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
class PlacesController
我在rails(3.0.5)/active\u scaffold\u vho(3.0.17)/ruby(1.9.2p180)虽然我以前从未见过这个ActiveScaffold异常,但我认为这就是发生的情况: 使用选项的:inverse\u设置反向关联,其效果仅在记录未保存时可见。这就是为什么在控制台实验中看不到它的原因 试试这个:
我不知道ActiveScaffold使用哪种构建/创建机制,但我猜如果他们抛出这个异常,他们可能会在内存中构建东西 因此,要解决此问题,请尝试:place = Place.new => #<Place ...> producer = place.producers.build => #<Producer ...> producer.place => nil place.producers.first.place => nil
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过去不支持类生产者
面,该限制过去在源代码中被注释。从Rails 3.1开始,这似乎是固定的。如图所示,如果使用:在
的反方向有多个上的
修改类,则控制台对内存中对象的实验应返回相关对象:inverse\u of
您可能需要查看上的Rails API文档。请显示您使用的控制台命令(避免出现问题),并显示遇到问题的控制器代码。如果显示错误代码(即控制器),将有助于诊断。如果你能做一个sql描述表来显示构建的内容,这也将有助于诊断。增加了你要求的精度。谢谢你的回答,我非常绝望(+1感谢你的时间)。你说的有道理,但行不通。我尝试了你的修复,同样的错误也出现了。奇怪的是,我从来没有通过一个地方创造一个制片人!更奇怪的是:其他类似的AS控制器也使用类似的关联。更奇怪的是:它过去是有用的。。。直到现在,我才发现它被破坏了(从来没有时间编写测试,现在我希望我能及时跳回去,把我以前的自己揍个粉碎,以惩罚这种轻率行为)@m_x你有没有尝试过在调试器中跟踪代码,和/或检查ActiveScaffold代码,看看它在哪里抛出这个异常?当我不知道更多的时候,我倾向于通读源代码和/或设置断点。RubyMine这样的编辑器在这方面很有帮助,因为您可以轻松浏览和搜索gems,而且调试器集成也非常棒。对不起,我帮不上忙…这就是我想要避免的。。。我已经花了一段可笑的时间调试AS中的另一个问题。我想我会从我做的任何其他新项目中退出,我厌倦了它的怪异行为。至于RubyMine,我之前已经考虑过购买个人许可证,但是我有点缺钱。无论如何,谢谢你抽出时间。哇,谢谢!你的回答肯定使我走上了正确的道路。因为我怀疑生产者的名字是罪魁祸首。我会编辑你的答案并接受它,因为你绝对值得赏金。@m_x:我们拒绝了你的答案,因为它的措辞是对帖子的回复;如果你想做一个更正,它应该读自然。Wolfram:可能有一些有趣的信息你想在里面编辑。
place = Place.new => #<Place ...> producer = place.producers.build => #<Producer ...> producer.place => nil place.producers.first.place => nil
validates_presence_of :place
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