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

**URL:** <https://www.ruby-forum.com/t/how-do-i-access-a-my-include-data-in-my-ruby-sql-results/169311>\
**Category:** Ruby\
**Created:** [June 2, 2009, 7:09am UTC](https://www.ruby-forum.com/t/how-do-i-access-a-my-include-data-in-my-ruby-sql-results/169311 "2009-06-02T07:09:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dustinbuss](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/53a042/32.png) [@dustinbuss](https://www.ruby-forum.com/u/dustinbuss)\
**Post date:** [June 2, 2009, 7:09am UTC](https://www.ruby-forum.com/t/how-do-i-access-a-my-include-data-in-my-ruby-sql-results/169311/1 "2009-06-02T07:09:02Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![dustinbuss](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/53a042/32.png) [@dustinbuss](https://www.ruby-forum.com/u/dustinbuss)\
**Post date:** [June 2, 2009, 10:04am UTC](https://www.ruby-forum.com/t/how-do-i-access-a-my-include-data-in-my-ruby-sql-results/169311/2 "2009-06-02T10:04:20Z")

</div>

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](http://rubyonrails.org)

---

<div class="post-metadata">

**Author:** ![dustinbuss](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/53a042/32.png) [@dustinbuss](https://www.ruby-forum.com/u/dustinbuss)\
**Post date:** [June 2, 2009, 11:31am UTC](https://www.ruby-forum.com/t/how-do-i-access-a-my-include-data-in-my-ruby-sql-results/169311/3 "2009-06-02T11:31:00Z")

</div>

Agreed that it is better to use the Rails list/group. But you probably  
want b.author.author\_name.
