Non-ActiveRecord based model being passed as string?

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?

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

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. [email protected]

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

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.

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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs