Forum: Ruby How do I access a my :include data in my Ruby SQL results

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
0784f92829c4eb4dcdd7cbfd5105af8c?d=identicon&s=25 DL Buss (dustinbuss)
on 2009-06-02 07:09
I am new to Ruby on Rails, so this is probably a very easy answer:

Two tables -- one of authors, one of books
authors belong_to books
In my situation, each book can only have one author.


I want to list all the books (with the author name) in a Select field.

@all_books = Book.find( :all, :include => [:authors] )

Then, I create the Select statement:

select(:book, :id, @all_books.collect {|b| [b.book_name + " (" +
b.author_name + ")", b.id ] }) %>


This creates an error because I'm trying to include the author_name.
When I run @all_books.inspect, I can see the author info in there.

So, how do I actually access the author data from that array (or object,
or whatever the correct terminology is)?

Thanks!
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (candlerb)
on 2009-06-02 10:04
DL Buss wrote:
> I am new to Ruby on Rails, so this is probably a very easy answer:

Rails (the web framework) is not the same as Ruby (the programming
language). So you would do much better to ask your question on a Rails
mailing list or forum. They are linked from rubyonrails.org
134ea397777886d6f0aa992672a50eaa?d=identicon&s=25 Mark Thomas (Guest)
on 2009-06-02 11:31
(Received via mailing list)
Agreed that it is better to use the Rails list/group. But you probably
want b.author.author_name.
This topic is locked and can not be replied to.