![](/img/trans.png)
[英]Rails accepts_nested_attributes_for with has_one association issue
[英]nil is not a symbol accepts_nested_attributes_for has_one association
我正在嘗試使用has_one
關聯來接受accepts_nested_attributes_for
。 我有以下使用mongoid的設置。
class Foo
include Mongoid::Document
include Mongoid::Timestamps
include Mongoid::Paranoia
has_and_belongs_to_many :bars, inverse_of: nil
end
class Bar
include Mongoid::Document
include Mongoid::Paranoia
has_one :magazine, class_name: 'Foo', dependent: :destroy, inverse_of: nil
accepts_nested_attributes_for :magazine
end
當我在Rails控制台中執行以下操作時,我似乎得到以下輸出。
r = Bar.new
=> #<Bar _id: 1213 ....
[2] pry(main)> r.foo.build TypeError: nil is not a symbol from
/Users/home/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems
/mongoid-3.1.6/lib/mongoid/attributes.rb:155:in `respond_to?
這篇博客文章是inverse_of
一個很好的文章。 您不必指定模型中的inverse_of: nil
,而必須指定一個符號。 例如, :bars
關聯應聲明為inverse_of: :magazine
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.