I'm trying to access my joined table data but alas..
For example association between authors and books, author have many books: I'm using join
funtion to join the author table to the books table in order to access the author's author_name
attribute
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>
Of course using the association would work: book_1.author.author_name
but that will require another query, which is what I'm trying to avoid.
I mean the joins
operation joins the author's data- there must be a way to access it right?
p.s. I can use includes
method. and eager load the author data as well, but since I'm just needing a single attribute- is there a way to accomplished it with only a joins
method? Thank you
Aucun commentaire:
Enregistrer un commentaire