【问题标题】:belongs_to association query method doesn't existbelongs_to 关联查询方法不存在
【发布时间】:2009-08-25 02:01:13
【问题描述】:

“协会?”当我创建belongs_to关联时Rails docs say应该存在的查询方法实际上并没有被创建:

class Author < ActiveRecord::Base
  has_many :posts
end
class Post < ActiveRecord::Base
  belongs_to :author
end
>> p = Post.create(:author => Author.create)
>> p.作者?
NoMethodError:未定义的方法“作者?”对于#<0x2555d28>

标签: ruby-on-rails associations


【解决方案1】:

把问号去掉。

p.author

【讨论】:

  • 当然可以,但问题是为什么“作者?”不行。有文件证明它应该这样做。
【解决方案2】:

它仅适用于布尔属性,而不适用于 belongs_to 关联。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多