# Trouble Using AR Object based on a Piggyback Query?

**URL:** <https://www.ruby-forum.com/t/trouble-using-ar-object-based-on-a-piggyback-query/54721>\
**Category:** Rails\
**Created:** [March 13, 2006, 6:13pm UTC](https://www.ruby-forum.com/t/trouble-using-ar-object-based-on-a-piggyback-query/54721 "2006-03-13T18:13:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jdw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dbc845/32.png) [@jdw](https://www.ruby-forum.com/u/jdw)\
**Post date:** [March 13, 2006, 6:13pm UTC](https://www.ruby-forum.com/t/trouble-using-ar-object-based-on-a-piggyback-query/54721/1 "2006-03-13T18:13:41Z")

</div>

Hi,

```
I'm new to Rails and Ruby and can't access methods/columns from an 

```

ActiveRecord object that is a “piggy-back” row as described by David  
here: [http://www.loudthinking.com/arc/000235.html](http://www.loudthinking.com/arc/000235.html)

```
Via Console I can create the piggyback object but reading attributes 

```

gives “TypeError: cannot convert String into Integer” (this is in  
Console and the browser). Some commented Console output is below, and  
any help is very much appreciated! (I can also post my model object if  
needed.)

# This is a regular old ActiveRecord object

> > obj =\> #\<ContractHc:0x22f40ec @attributes={“hc\_zip”=\>nil, “coi\_signature”=\>“asd”, “hc\_address\_one”=\>“16 Wetfoot Road”, “hc\_organization”=\>“GFH Inc.”, “coi\_signature\_date”=\>“2006-03-08 13:22:10”, “id\_people”=\>“1”, “id\_projects”=\>“1”, “hc\_ssn”=\>“”, “hc\_recipient\_name”=\>“asd”, “hc\_department”=\>“Posse HFR”, “hc\_ip”=\>“127.0.0.1”, “id\_t\_panel\_members”=\>“1”, “id\_panel\_members”=\>“1”, “hc\_accepts\_honoraria”=\>“0”, “hc\_region”=\>“Wild West”, “id\_states”=\>“1”, “hc\_signature\_email”=\>“removed\_email\_address@domain.invalid”, “hc\_verification\_code”=\>“123456”, “hc\_address\_two”=\>“Apartment B”, “hc\_signature\_date”=\>“2006-03-11 21:25:07”, “hc\_city”=\>“Carson City”, “id\_panels”=\>“1”, “id\_countries”=\>“1”, “hc\_signature”=\>“asd”}\>

# Here I’m accessing it’s data fine…

> > obj[“hc\_city”] =\> “Carson City”

# This is an ActiveRecord object which uses a hand-written piggyback

query in the model (the query is working fine as you can see…)

> > obj2 =\> [#\<ContractHc:0x22e8e2c @attributes={“city”=\>“Carson City”, “zip”=\>“22122”, “region”=\>“Wild West”, “dep\_name\_short”=\>“Posse HFR”, “kind”=\>“Active”, “pnl\_name\_long”=\>“New Mexico and Texas Posse”, “org\_name\_short”=\>“GFH Inc.”, “ssn”=\>“221-34-6720”, “prj\_name\_long”=\>“Wild West Remediation 2006”, “id\_panel\_members”=\>“1”, “address\_two”=\>“Apartment B”, “address\_email”=\>“removed\_email\_address@domain.invalid”, “id\_states”=\>“1”, “id\_countries”=\>“1”, “address\_one”=\>“16 Wetfoot Road”, “name\_first”=\>“Jesse”, “name\_last”=\>“James”, “text\_hon”=\>“This is your db-derived hon text.”}\>]

# My error is here: I can’t get data out/access properties for some

reason…

> > obj2[“city”] TypeError: cannot convert String into Integer  
> > from (irb):65:in `[]’  
> > from (irb):65

(Btw, I’ve used Lasso, ASP3, Php, JSP/Servelets and am _really_ enjoying  
Rails.)

```
JW

```

---

<div class="post-metadata">

**Author:** ![jdw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dbc845/32.png) [@jdw](https://www.ruby-forum.com/u/jdw)\
**Post date:** [March 13, 2006, 7:00pm UTC](https://www.ruby-forum.com/t/trouble-using-ar-object-based-on-a-piggyback-query/54721/2 "2006-03-13T19:00:21Z")

</div>

Hi,

A little more looking and I’ve seen that obj (the standard AR record) is  
an object of the type ContractHc, while obj2 is of type Array. The []  
below show this as well, I suppose.

So my question now is why am I getting back two differrent types of  
objects? This doesn’t seem to be the expected behavior described for a  
piggyback query at all.

Thanks,

JDW

> > > obj =\> #\<ContractHc:0x22f40ec @attributes={“hc\_zip”=\>nil, “coi\_signature”=\>“asd”, “hc\_address\_one”=\>“16 Wetfoot Road”, “hc\_organization”=\>“GFH Inc.”, “coi\_signature\_date”=\>“2006-03-08 13:22:10”, “id\_people”=\>“1”, “id\_projects”=\>“1”, “hc\_ssn”=\>"", “hc\_recipient\_name”=\>“asd”, “hc\_department”=\>“Posse HFR”, “hc\_ip”=\>“127.0.0.1”, “id\_t\_panel\_members”=\>“1”, “id\_panel\_members”=\>“1”, “hc\_accepts\_honoraria”=\>“0”, “hc\_region”=\>“Wild West”, “id\_states”=\>“1”, “hc\_signature\_email”=\>“removed\_email\_address@domain.invalid”, “hc\_verification\_code”=\>“123456”, “hc\_address\_two”=\>“Apartment B”, “hc\_signature\_date”=\>“2006-03-11 21:25:07”, “hc\_city”=\>“Carson City”, “id\_panels”=\>“1”, “id\_countries”=\>“1”, “hc\_signature”=\>“asd”}\>

> > > obj2 =\> [#\<ContractHc:0x22e8e2c @attributes={“city”=\>“Carson City”, “zip”=\>“22122”, “region”=\>“Wild West”, “dep\_name\_short”=\>“Posse HFR”, “kind”=\>“Active”, “pnl\_name\_long”=\>“New Mexico and Texas Posse”, “org\_name\_short”=\>“GFH Inc.”, “ssn”=\>“221-34-6720”, “prj\_name\_long”=\>“Wild West Remediation 2006”, “id\_panel\_members”=\>“1”, “address\_two”=\>“Apartment B”, “address\_email”=\>“removed\_email\_address@domain.invalid”, “id\_states”=\>“1”, “id\_countries”=\>“1”, “address\_one”=\>“16 Wetfoot Road”, “name\_first”=\>“Jesse”, “name\_last”=\>“James”, “text\_hon”=\>“This is your db-derived hon text.”}\>]

---

<div class="post-metadata">

**Author:** ![jdw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dbc845/32.png) [@jdw](https://www.ruby-forum.com/u/jdw)\
**Post date:** [March 13, 2006, 7:13pm UTC](https://www.ruby-forum.com/t/trouble-using-ar-object-based-on-a-piggyback-query/54721/3 "2006-03-13T19:13:45Z")

</div>

Ok, just FYI what I’m gettting back is a single AR base object for obj,  
and an array of AR base objects for obj2. I suppose this is to be  
expected, and obj2[0][“city”] will of course reference the data.

JW

---

<div class="post-metadata">

**Author:** ![jdw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dbc845/32.png) [@jdw](https://www.ruby-forum.com/u/jdw)\
**Post date:** [March 13, 2006, 8:06pm UTC](https://www.ruby-forum.com/t/trouble-using-ar-object-based-on-a-piggyback-query/54721/4 "2006-03-13T20:06:30Z")

</div>

it seems you have answered your own question but i just wanted to make  
one  
comment.

in the future you might want to reconsider posting name/address/ssn  
information to a public mailing list. i realize the information might  
be  
dummy data, and if that is the case, feel free to ignore the comment.

just an FYI.

---

<div class="post-metadata">

**Author:** ![jdw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dbc845/32.png) [@jdw](https://www.ruby-forum.com/u/jdw)\
**Post date:** [March 13, 2006, 8:41pm UTC](https://www.ruby-forum.com/t/trouble-using-ar-object-based-on-a-piggyback-query/54721/5 "2006-03-13T20:41:48Z")

</div>

Hi Chris,

Thanks for the note, which is a good point! The data is in fact dummy  
data and as such you can e-mail Jesse James at removed\_email\_address@domain.invalid for  
proof 😉

Cheers!
