ok, using the authors/books example
class Author < ActiveRecord::Base
has_and_belongs_to_many :books
end
class Book < ActiveRecord::Base
has_and_belongs_to_many :authors
end
join table is authors_books with an extra field called “read_at” which
is a
datetime column
book = Book.create(:title => “The Life of Foobars”)
bob = Author.new(:name => “bob”)
bill = Author.new(:name => "bill)
book.authors.push_with_attributes(bob, :read_at => Time.now)
book.authors.push_with_attributes(bill, :read_at => Time.now)
so now we’ve created the associations between books and authors
if you do:
book = Book.find(1)
book.authors.each { |a| puts a.read_at }
you get what i described in my previous reply…but if you do:
book = Book.find(1, :include => :authors)
book.authors.each { |a| puts a.read_at }
you get NoMethodError as read_at was not included in the author objects
the generated SQL from the first example is:
SELECT * FROM books WHERE (books.id = 1) LIMIT 1
SELECT * FROM authors LEFT JOIN authors_books ON authors.id =
authors_books.author_id WHERE (authors_books.book_id = 1 )
and the second example is:
SELECT authors.id AS t1_r0, authors.name AS t1_r1, books.id AS t0_r0,
books.title AS t0_r1 FROM books LEFT OUTER JOIN authors_books ON
authors_books.book_id = books.id LEFT OUTER JOIN authors ON
authors_books.author_id = authors.id WHERE (books.id = 1)
so i guess if you want to access the extra fields, you’ll have to go
with
the first example.
I’ll ask on the list if this is the expected behavior.
On 11/30/05, David M. [email protected] wrote:
Doesn’t work for me - I get an error along the lines of
‘extra_field_from_join_table’ is a missing method…
If it works for you, could you please share how you’ve got your models
defined?
Dave M.