2009-08-25 44 views
1

「關聯?」該Rails docs say當我創建一個belongs_to的關聯應該存在的查詢方法實際上並沒有獲得創建:belongs_to關聯查詢方法不存在

class Author < ActiveRecord::Base 
    has_many :posts 
end 
class Post < ActiveRecord::Base 
    belongs_to :author 
end 
>> p = Post.create(:author => Author.create) 
>> p.author? 
NoMethodError: undefined method `author?' for #<Post:0x2555d28>

這是一個錯誤,是在文檔錯了,還是我做錯了什麼?

+0

嗨你試圖達到什麼目標?看看是否有作者與這篇文章混淆? – Staelen 2009-08-25 03:45:03

+0

是的,但具體使用「帖子#作者?」方法在文檔中描述。我意識到我可以通過其他方式獲得這些信息(例如布爾上下文中的p.author),但我試圖找出爲什麼記錄的功能不適合我。 – KingPong 2009-08-25 21:08:26

+0

我無法讓它工作,看起來像一個bug ... – Staelen 2009-08-26 07:07:16

回答

1

取下問號。

p.author 
+0

當然,這有效,但問題是爲什麼「作者?」不行。記錄它應該。 – KingPong 2009-08-25 03:32:20

1

它只適用於布爾屬性,不適用於belongs_to關聯。

相關問題