1
我試圖訪問我的連接表數據,但可惜..Rails3中:使用連接表的數據
對於作者和圖書,作者之間的示例關聯有很多的書: 我使用join
funtion加盟筆者表books表,以便使用該協會將努力進入筆者的author_name
屬性
class Author < ActiveRecord::Base
has_many :books
attr_accessible :author_name
end
class Book < ActiveRecord::Base
belongs_to :author
end
random_author = Author.first
books = random_author.books.where(id > 5).joins(:author) #this should make all author attributes available to each book, right?
book_1 = books.first
book_1.author_name
=> NoMethodError: undefined method `author_name' for #<Book:0x111111>
當然:book_1.author.author_name
但需要另一個查詢,這是我想要避免的。
我的意思是joins
操作加入作者的數據 - 必須有一種方法來訪問它的權利?
p.s.我可以使用includes
方法。也渴望加載作者的數據,但由於我只需要一個屬性 - 有沒有辦法只用joins
方法來完成呢?謝謝