Forum: Ruby on Rails ActiveRecord#to_param

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
B263ed6ba137a0cd968d35d1476a9a3c?d=identicon&s=25 Torsten Robitzki (Guest)
on 2006-05-17 20:56
(Received via mailing list)
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
24d2f8804e6bb4b7ea6bd11e0a586470?d=identicon&s=25 Jeremy Kemper (Guest)
on 2006-05-17 21:22
(Received via mailing list)
On May 17, 2006, at 11:57 AM, Torsten Robitzki 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?
   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
B263ed6ba137a0cd968d35d1476a9a3c?d=identicon&s=25 Torsten Robitzki (Guest)
on 2006-05-17 21:34
(Received via mailing list)
Hi Jeremy,

Jeremy Kemper wrote:
> On May 17, 2006, at 11:57 AM, Torsten Robitzki 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?
>   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
5d15c6821f3c3054c04b85471824ba7c?d=identicon&s=25 steve ross (Guest)
on 2006-05-17 22:22
(Received via mailing list)
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...
B263ed6ba137a0cd968d35d1476a9a3c?d=identicon&s=25 Torsten Robitzki (Guest)
on 2006-05-19 20:40
(Received via mailing list)
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
5d15c6821f3c3054c04b85471824ba7c?d=identicon&s=25 Kevin Olbrich (Guest)
on 2006-05-19 21:42
(Received via mailing list)
On Friday, May 19, 2006, at 8:40 PM, Torsten Robitzki wrote:
>> of the length of the URL.
>Torsten
>
>
>_______________________________________________
>Rails mailing list
>Rails@lists.rubyonrails.org
>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
B263ed6ba137a0cd968d35d1476a9a3c?d=identicon&s=25 Torsten Robitzki (Guest)
on 2006-05-20 20:08
(Received via mailing list)
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
This topic is locked and can not be replied to.