# ActiveRecord#to\_param

**URL:** <https://www.ruby-forum.com/t/activerecord-to-param/60833>\
**Category:** Rails\
**Created:** [May 17, 2006, 8:56pm UTC](https://www.ruby-forum.com/t/activerecord-to-param/60833 "2006-05-17T20:56:52Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![torsten](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8e8cbc/32.png) [@torsten](https://www.ruby-forum.com/u/torsten)\
**Post date:** [May 17, 2006, 8:56pm UTC](https://www.ruby-forum.com/t/activerecord-to-param/60833/1 "2006-05-17T20:56:52Z")

</div>

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 😉

best regards  
Torsten

---

<div class="post-metadata">

**Author:** ![torsten](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8e8cbc/32.png) [@torsten](https://www.ruby-forum.com/u/torsten)\
**Post date:** [May 17, 2006, 9:22pm UTC](https://www.ruby-forum.com/t/activerecord-to-param/60833/2 "2006-05-17T21:22:15Z")

</div>

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](http://api.rubyonrails.com/classes/ActiveRecord/Locking.html)

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

---

<div class="post-metadata">

**Author:** ![torsten](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8e8cbc/32.png) [@torsten](https://www.ruby-forum.com/u/torsten)\
**Post date:** [May 17, 2006, 9:34pm UTC](https://www.ruby-forum.com/t/activerecord-to-param/60833/3 "2006-05-17T21:34:46Z")

</div>

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](http://api.rubyonrails.com/classes/ActiveRecord/Locking.html)

no. ☹

> 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

---

<div class="post-metadata">

**Author:** ![torsten](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8e8cbc/32.png) [@torsten](https://www.ruby-forum.com/u/torsten)\
**Post date:** [May 17, 2006, 10:22pm UTC](https://www.ruby-forum.com/t/activerecord-to-param/60833/4 "2006-05-17T22:22:21Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![torsten](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8e8cbc/32.png) [@torsten](https://www.ruby-forum.com/u/torsten)\
**Post date:** [May 19, 2006, 8:40pm UTC](https://www.ruby-forum.com/t/activerecord-to-param/60833/5 "2006-05-19T20:40:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![torsten](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8e8cbc/32.png) [@torsten](https://www.ruby-forum.com/u/torsten)\
**Post date:** [May 19, 2006, 9:42pm UTC](https://www.ruby-forum.com/t/activerecord-to-param/60833/6 "2006-05-19T21:42:28Z")

</div>

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

> > of the length of the URL.  
> > Torsten
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](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

---

<div class="post-metadata">

**Author:** ![torsten](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8e8cbc/32.png) [@torsten](https://www.ruby-forum.com/u/torsten)\
**Post date:** [May 20, 2006, 8:08pm UTC](https://www.ruby-forum.com/t/activerecord-to-param/60833/7 "2006-05-20T20:08:18Z")

</div>

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
