ActiveRecord#to_param

Hello,
to implement some some kind of optimistic locking, I want to build a
view, where an object is displayed together with a link containing the
data of the displayed object. My first try looked something like

<%= object.object_discription %>
<%= link_to “action”, :action => “take_action”, :data => object %>

this won’t work because object is of a class derived from ActiveRecord
and thus only the object.id will be transmitted. Next I’ve tried some
approaches to stringinize or urlizie the contents of object and read and
searching the documentation. The closed I came up with was:

<%= link_to “action”, {:action =>
“take_action”}.merge(object.attributes_before_type_cast) %>

This way I can see the data of the object in the @params Hash but
instead of the needed values “data” => {“attr1” => “value1”, “attr2” =>
“value2”} I get a more flattened Hash: {“attr1” => “value1”, “attr2” =>
“value2”}. I think I might solve the problem by appending a “[data]” to
every key of the object.attributes_before_type_cast hash.

Is there already a solution for my problem in Rails? Thanks for reading
this longish text :wink:

best regards
Torsten

On May 17, 2006, at 11:57 AM, Torsten R. wrote:

Hello,
to implement some some kind of optimistic locking, I want to build
a view, where an object is displayed together with a link
containing the data of the displayed object. My first try looked
something like

Hi Torsten - have you tried the bundled optimistic locking?
Peak Obsession

It uses a lock_version column rather than comparing existing fields.
You pass the lock_version along with your form data, query params, or
even as a session var if you don’t want to muddy your URLs.

jeremy

Hi Jeremy,

Jeremy K. wrote:

On May 17, 2006, at 11:57 AM, Torsten R. wrote:

Hello,
to implement some some kind of optimistic locking, I want to build a
view, where an object is displayed together with a link containing
the data of the displayed object. My first try looked something like

Hi Torsten - have you tried the bundled optimistic locking?
Peak Obsession

no. :frowning:

It uses a lock_version column rather than comparing existing fields.
You pass the lock_version along with your form data, query params, or
even as a session var if you don’t want to muddy your URLs.

This would solve my problem much better, thank you for this hint.
Meanwhile I’ve tried my suggested solution and find that it will work
(except that the field names have to be between []).

But I’m going to use the solution with the update counter.

best regards
Torsten

I’ve never tried this, but it sounds like you want to marshal the data
prior to linking it. This may not be practical in a GET request because
of the length of the URL. Look into Marshal.dump and Marshal.load. For a
bit of extra security, you might use code like:

my_obj = Marshal.load(Base64.encode64(@the_obj.dup) unless @the_obj.nil?
rescue ‘’
<% link_to “action”, :action => “take_action”, :data => my_obj %>

See if that works…

Hi Steve,

steve ross wrote:

I’ve never tried this, but it sounds like you want to marshal the data
prior to linking it.

yes, that is what I’ve tried. I just had the idea to use the same
protocol that rails is using for marshalling data.

This may not be practical in a GET request because
of the length of the URL.

I have quit small objects here, but who knows, there are good chances
that they will grow.

Look into Marshal.dump and Marshal.load. For a
bit of extra security, you might use code like:

Interesting, thanks.

best regards,
Torsten

On Friday, May 19, 2006, at 8:40 PM, Torsten R. wrote:

of the length of the URL.
Torsten


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

Personally, I would use the lock_version column, but if you really want
another option you could try this.

key = Digest::MD5(object.to_yaml)

this will generate a MD5 digest that is unique to the contents of the
object at the time you call it. You can check it again before you save,
if it’s different, someone has changed the record.

The length of this key will always be the same, so you can use it for
any object. If you want to ignore certain fields, you can use your own
method instead of to_yaml.

_Kevin

Hi Kevin,

Personally, I would use the lock_version column, but if you really want
another option you could try this.

key = Digest::MD5(object.to_yaml)

I will go the lock_version column way, but I will keep your idea in
mind. This approach could be quit useful if it’s not possible to change
the used data base.

best regards
Torsten