# Why all columns of joined table get instantiated as String?

**URL:** <https://www.ruby-forum.com/t/why-all-columns-of-joined-table-get-instantiated-as-string/184563>\
**Category:** Rails\
**Created:** [March 4, 2010, 3:58pm UTC](https://www.ruby-forum.com/t/why-all-columns-of-joined-table-get-instantiated-as-string/184563 "2010-03-04T15:58:54Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![oshanter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/2acd7d/32.png) [@oshanter](https://www.ruby-forum.com/u/oshanter)\
**Post date:** [March 4, 2010, 3:58pm UTC](https://www.ruby-forum.com/t/why-all-columns-of-joined-table-get-instantiated-as-string/184563/1 "2010-03-04T15:58:54Z")

</div>

Been developing in Rails for about a month and I’m still working on  
grasping its subtleties.  
Can someone please shed some light on this simple scenario?

## Schema

create\_table :books do |t|  
t.string :title  
t.integer :year  
end

create\_table :pages do |t|  
t.integer :no  
t.references :book  
end

## Models

Book has\_many :pages  
Page belongs\_to :book

Then in Rails’ console:

@pages = Page.all(  
:select =\> ‘pages.id, pages.no, books.year book\_year’,  
:joins =\> :book  
)

@pages.first.no.class #=\> Fixnum  
@pages.first.book\_year.class #=\> String

What I don’t understand is why book\_year is a String instead of a  
Fixnum. Is it possible to get Book’s columns correctly instantiated?

Thanks

---

<div class="post-metadata">

**Author:** ![oshanter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/2acd7d/32.png) [@oshanter](https://www.ruby-forum.com/u/oshanter)\
**Post date:** [March 4, 2010, 5:41pm UTC](https://www.ruby-forum.com/t/why-all-columns-of-joined-table-get-instantiated-as-string/184563/2 "2010-03-04T17:41:04Z")

</div>

On Mar 4, 2:58 pm, Jack S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> What I don’t understand is why book\_year is a String instead of a  
> Fixnum. Is it possible to get Book’s columns correctly instantiated?

Not that I know of. The typecasting stuff only knows about the columns  
of the model itself.

Fred

---

<div class="post-metadata">

**Author:** ![oshanter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/2acd7d/32.png) [@oshanter](https://www.ruby-forum.com/u/oshanter)\
**Post date:** [March 5, 2010, 3:07pm UTC](https://www.ruby-forum.com/t/why-all-columns-of-joined-table-get-instantiated-as-string/184563/3 "2010-03-05T15:07:00Z")

</div>

Thanks, Fred. I suppose I’ll stick with views and conversions.

---

<div class="post-metadata">

**Author:** ![oshanter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/2acd7d/32.png) [@oshanter](https://www.ruby-forum.com/u/oshanter)\
**Post date:** [March 5, 2010, 7:17pm UTC](https://www.ruby-forum.com/t/why-all-columns-of-joined-table-get-instantiated-as-string/184563/4 "2010-03-05T19:17:11Z")

</div>

On Fri, Mar 5, 2010 at 9:07 AM, Jack S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Thanks, Fred. I suppose I’ll stick with views and conversions.

Why not

pages = Page.all(:include =\> :book)

pages.first.book.year

–  
Rick DeNatale

Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
Twitter: [http://twitter.com/RickDeNatale](http://twitter.com/RickDeNatale)  
WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
LinkedIn: [http://www.linkedin.com/in/rickdenatale](http://www.linkedin.com/in/rickdenatale)

---

<div class="post-metadata">

**Author:** ![oshanter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/2acd7d/32.png) [@oshanter](https://www.ruby-forum.com/u/oshanter)\
**Post date:** [March 6, 2010, 2:01am UTC](https://www.ruby-forum.com/t/why-all-columns-of-joined-table-get-instantiated-as-string/184563/5 "2010-03-06T02:01:37Z")

</div>

On Fri, Mar 5, 2010 at 2:12 PM, Jack S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> :conditions =\> “books.library\_id = #{1}”  
> )
> 
> AR will generate:  
> SELECT pages.id, pages.no FROM “pages” INNER JOIN “books” ON “books”.id  
> = “pages”.book\_id WHERE (books.library\_id = 1)
> 
> So the join has already been done. Wouldn’t :include make a redundant  
> query?

Then just change the joins to include:

Page.all(  
:include =\> :book,  
:conditions =\> [“books.library\_id = ?”, 1]  
)

## Which will both allow you to refer to the books fields in the where clause, and return an collection of object graphs rather than page object corrupted with fields they shouldn’t have.

Rick DeNatale

Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
Twitter: [http://twitter.com/RickDeNatale](http://twitter.com/RickDeNatale)  
WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
LinkedIn: [http://www.linkedin.com/in/rickdenatale](http://www.linkedin.com/in/rickdenatale)

---

<div class="post-metadata">

**Author:** ![oshanter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/2acd7d/32.png) [@oshanter](https://www.ruby-forum.com/u/oshanter)\
**Post date:** [March 6, 2010, 6:49pm UTC](https://www.ruby-forum.com/t/why-all-columns-of-joined-table-get-instantiated-as-string/184563/6 "2010-03-06T18:49:54Z")

</div>

Rick Denatale wrote:

> Then just change the joins to include:
> 
> Page.all(  
> :include =\> :book,  
> :conditions =\> [“books.library\_id = ?”, 1]  
> )
> 
> Which will both allow you to refer to the books fields in the where  
> clause, and return an collection of object graphs rather than page  
> object corrupted with fields they shouldn’t have.

That’s true, but I lose control over :select

Page.all(  
:select =\> ‘pages.id, pages.no’,  
:include =\> :book,  
:conditions =\> [“books.library\_id = ?”, 1]  
)

…will generate:

SELECT  
“pages”.“id” AS t0\_r0,  
“pages”.“no” AS t0\_r1,  
“pages”.“book\_id” AS t0\_r2,  
“pages”.“created\_at” AS t0\_r3,  
“pages”.“updated\_at” AS t0\_r4,  
“books”.“id” AS t1\_r0,  
“books”.“title” AS t1\_r1,  
“books”.“year” AS t1\_r2,  
“books”.“library\_id” AS t1\_r3,  
“books”.“created\_at” AS t1\_r4,  
“books”.“updated\_at” AS t1\_r5  
FROM  
“pages”  
LEFT OUTER JOIN “books” ON “books”.id = “pages”.book\_id  
WHERE  
(books.library\_id = 1)

The real “books” and “pages” have decimal and large text columns that  
are not always needed.

---

<div class="post-metadata">

**Author:** ![oshanter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/2acd7d/32.png) [@oshanter](https://www.ruby-forum.com/u/oshanter)\
**Post date:** [March 6, 2010, 8:48pm UTC](https://www.ruby-forum.com/t/why-all-columns-of-joined-table-get-instantiated-as-string/184563/7 "2010-03-06T20:48:17Z")

</div>

On 6 March 2010 17:49, Jack S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> The real “books” and “pages” have decimal and large text columns that  
> are not always needed.

Could be worth considering extracting those fields into another table  
and have a 1:1 link… a little messy, but saves you returning lots of  
data when you only need a small part of it.

---

<div class="post-metadata">

**Author:** ![oshanter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/2acd7d/32.png) [@oshanter](https://www.ruby-forum.com/u/oshanter)\
**Post date:** [March 5, 2010, 8:12pm UTC](https://www.ruby-forum.com/t/why-all-columns-of-joined-table-get-instantiated-as-string/184563/8 "2010-03-05T20:12:34Z")

</div>

Rick Denatale wrote:

> Why not
> 
> pages = Page.all(:include =\> :book)

I usually need to filter by, let’s say, a certain library:

@pages = Page.all(  
:select =\> ‘pages.id, pages.no’,  
:joins =\> :book,  
:conditions =\> “books.library\_id = #{1}”  
)

AR will generate:  
SELECT pages.id, pages.no FROM “pages” INNER JOIN “books” ON “books”.id  
= “pages”.book\_id WHERE (books.library\_id = 1)

So the join has already been done. Wouldn’t :include make a redundant  
query?
