# Non-ActiveRecord based model being passed as string?

**URL:** <https://www.ruby-forum.com/t/non-activerecord-based-model-being-passed-as-string/176096>\
**Category:** Rails\
**Created:** [September 24, 2009, 7:42pm UTC](https://www.ruby-forum.com/t/non-activerecord-based-model-being-passed-as-string/176096 "2009-09-24T19:42:23Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![nozavroni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f17d59/32.png) [@nozavroni](https://www.ruby-forum.com/u/nozavroni)\
**Post date:** [September 24, 2009, 7:42pm UTC](https://www.ruby-forum.com/t/non-activerecord-based-model-being-passed-as-string/176096/1 "2009-09-24T19:42:23Z")

</div>

I have a model that looks like this (with various other methods)

class HsdPair  
attr\_accessor :hsd

def initialize(hsd)  
@hsd = hsd  
@current\_display = 0  
@has\_changed\_flag = false  
end  
end

After initializing this object in the controller

@hsd\_pairs = HsdPair.new(@job.hsd\_pairs)

using it in the view various times and in various ways, when I try to  
pass this object back to the controller for various ajax interactions  
like so:  
\<%= link\_to\_remote image\_tag(“right-arrow.png”, :size =\> “48x48”),  
:url =\> {  
:action =\> “hsd\_forward”,  
:hsd\_pairs =\> hsd\_pairs },  
:with =\> “‘update=’+$(‘current\_display’).value” %\>

it shows up as being passed as a string:

Parameters:  
{“authenticity\_token”=\>“UGj1tJruzDdzBGexIZCLyYluQINn2Wl9pADFQ7G1SY4=”,  
“hsd\_pairs”=\>"#HsdPair:0xb1c4fc14", “update”=\>“0”}

Any idea on how to solve this or to get the data back from that pointer?  
Thanks.

Alternatively, does anyone know if PassiveRecord solves this problem?

---

<div class="post-metadata">

**Author:** ![nozavroni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f17d59/32.png) [@nozavroni](https://www.ruby-forum.com/u/nozavroni)\
**Post date:** [September 24, 2009, 8:13pm UTC](https://www.ruby-forum.com/t/non-activerecord-based-model-being-passed-as-string/176096/2 "2009-09-24T20:13:16Z")

</div>

Luke van der Hoeven wrote:

> Any idea on how to solve this or to get the data back from that pointer?  
> Thanks.
> 
> Alternatively, does anyone know if PassiveRecord solves this problem?

First of all, it’s not a pointer…  
Secondly, the object is being passed from a web client most probably on  
another host back to your web server… how could it be resolved by  
de-referencing a pointer?

NOT TESTED… just ideas to get you started…

If you make your initialize method look like this:

class HsdPair

def initialize(hsd)  
if(hsd.kind\_of? Map)  
@current\_display = hsd[:current\_display] || 0  
@has\_changed\_flag = hsd[:has\_changed\_flag] || false  
@hsd = hsd[:hsd] || raise “hsd attribute missing”  
else  
@current\_display = 0  
@has\_changed\_flag = false  
@hsd = hsd  
end  
end

# Much more elegant ways to do this but not sure if you are on 1.8 or

1.9  
def to\_params()  
{:current\_display =\> @current\_display, :has\_changed\_flag =\>  
@has\_changed\_flag, :hsd =\> @hsd}  
end  
end

Then you can reconstruct the object on the server side by calling  
HsdPair.new(params)

hth

ilan

---

<div class="post-metadata">

**Author:** ![nozavroni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f17d59/32.png) [@nozavroni](https://www.ruby-forum.com/u/nozavroni)\
**Post date:** [September 24, 2009, 11:25pm UTC](https://www.ruby-forum.com/t/non-activerecord-based-model-being-passed-as-string/176096/3 "2009-09-24T23:25:33Z")

</div>

When ERB compiles your view, the result is a string. If I remember  
correctly, the to\_s method is invoked on objects, which in this case  
results in the string that you are seeing. You need to pass in an  
identifier that can be used to look up the specific instance of  
HsdPair, or alternatively, use a low cost db like sqlite and pass in  
the id.

On Sep 24, 1:42 pm, Luke L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![nozavroni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f17d59/32.png) [@nozavroni](https://www.ruby-forum.com/u/nozavroni)\
**Post date:** [September 25, 2009, 2:39pm UTC](https://www.ruby-forum.com/t/non-activerecord-based-model-being-passed-as-string/176096/4 "2009-09-25T14:39:30Z")

</div>

Luke van der Hoeven wrote:

> what does the constructor method do differently in 1.8 vs. 1.9? Does it  
> exist in 1.8 or 1.9? Currently working with 1.8.x so I’m not sure my  
> constructors will be able to do it without that extra code.

It wasnt’ the initialize() method that I didn’t like, it was the  
to\_params() non-dry hack which made me feel a little ill…

The method I provided is not maintainable as adding attributes to the  
class will break the ‘serialization’ mechanism… a much better solution  
would be to use reflection to run through your attributes and map them  
(which is slightly different in 1.9 as compared to 1.8)

ilan

---

<div class="post-metadata">

**Author:** ![nozavroni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f17d59/32.png) [@nozavroni](https://www.ruby-forum.com/u/nozavroni)\
**Post date:** [September 24, 2009, 10:58pm UTC](https://www.ruby-forum.com/t/non-activerecord-based-model-being-passed-as-string/176096/5 "2009-09-24T22:58:31Z")

</div>

Ilan B. wrote:

> # Much more elegant ways to do this but not sure if you are on 1.8 or
> 
> 1.9  
> def to\_params()  
> {:current\_display =\> @current\_display, :has\_changed\_flag =\>  
> @has\_changed\_flag, :hsd =\> @hsd}  
> end  
> end
> 
> Then you can reconstruct the object on the server side by calling  
> HsdPair.new(params)

what does the constructor method do differently in 1.8 vs. 1.9? Does it  
exist in 1.8 or 1.9? Currently working with 1.8.x so I’m not sure my  
constructors will be able to do it without that extra code.

Long story short, this method worked. Didn’t figure it’d be as hard as  
all that, but I suppose without pulling from the db with ActiveRecord,  
persistence has to be manufactured.

Thanks for the help.

---

<div class="post-metadata">

**Author:** ![nozavroni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f17d59/32.png) [@nozavroni](https://www.ruby-forum.com/u/nozavroni)\
**Post date:** [September 25, 2009, 3:55pm UTC](https://www.ruby-forum.com/t/non-activerecord-based-model-being-passed-as-string/176096/6 "2009-09-25T15:55:52Z")

</div>

Ilan B. wrote:

> The method I provided is not maintainable as adding attributes to the  
> class will break the ‘serialization’ mechanism… a much better solution  
> would be to use reflection to run through your attributes and map them  
> (which is slightly different in 1.9 as compared to 1.8)
> 
> ilan

How would this be done, in a very basic way, when this table is not  
backed by ActiveRecord? The reflection class is provided by ActiveRecord  
and this is a table-less data structure right now. Hence the weird need  
to pass stuff around. I would put it in the db, but this is data being  
pulled from a database and remapped in a way, as we’re using a non-rails  
mapped oracle db. My app has to tie into existing data, so its a bit of  
a problem.

Thanks for all the help so far.
