Trouble Using AR Object based on a Piggyback Query?


#1

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

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

#2

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.”}>]


#3

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


#4

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.


#5

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 :wink:

Cheers!