Passing objects between actions/controllers

Hello!
I’ve just started with RoR a few days ago, so please excuse my lack of
knowledge :slight_smile:

I have this situation: customer sends search request to DB, gets a list
of matching items back. Each item has a link to processing action.

With my limited knowledge i have gotten the link to pass the ID of it’s
parent object to processing action, where the object is requested again,
based on the ID recieved. My code as follows:
<% for objct in @found %>
<% form_remote_tag :url => { :action => :set_choice, :id => objct } do
%>
<%= submit_tag “Submit” %>
<% end %>
<% end %>

What i need to do is pass the object whose link has been clicked as
parameter to the processing action - it would remove the need for second
request to the DB, increasing the overall performance.

Is there a way to do this? Thanks in advance :slight_smile:

Also, i forgot to mention, i would like to know the same about passing
objects between two controllers.

On 9/20/07, Mind M. [email protected] wrote:

based on the ID recieved. My code as follows:

Is there a way to do this? Thanks in advance :slight_smile:

The general answer is “no”, because the request that retrieves the
list and the request that processes the selected item are totally
separate. The only data that gets “passed” is the text strings that
are sent to the browser and sent back (in the params hash) when the
user submits the form or clicks a link.

So the typical idiom in the second action would be:

def set_choice
@object = MyModel.find(params[:id])
end

Yes, that requires a db fetch (but it’s quick).

If you really want to avoid the second query, you have two basic
choices:

  • Stash the object on the server between requests. The typical way is
    to save it in the session, since it automatically
    serializes/deserializes the object for you.
  • Serialize the object and include it in the form somewhere, and
    deserialize it from the parameters in the next request.

The session technique is easier and safer. (The other technique is
risky, because the client could tamper with the object before
returning it). But, you have additional problems:

  • How do you keep the stash cleaned up?
  • What if the object is destroyed between request 1 and request 2?
  • You now cannot have a request 2 without a preceeding request 1 to
    load the stash

My advice is to follow the normal idioms and not worry about
“performance” until it becomes an issue. There’s lots of things you
can do to improve performance without resorting to these kinds of
techniques.

Mind M. wrote:

Hello!
I’ve just started with RoR a few days ago, so please excuse my lack of
knowledge :slight_smile:

I have this situation: customer sends search request to DB, gets a list
of matching items back. Each item has a link to processing action.

With my limited knowledge i have gotten the link to pass the ID of it’s
parent object to processing action, where the object is requested again,
based on the ID recieved. My code as follows:
<% for objct in @found %>
<% form_remote_tag :url => { :action => :set_choice, :id => objct } do
%>
<%= submit_tag “Submit” %>
<% end %>
<% end %>

What i need to do is pass the object whose link has been clicked as
parameter to the processing action - it would remove the need for second
request to the DB, increasing the overall performance.

Is there a way to do this? Thanks in advance :slight_smile:

I can think of two ways, neither of which is a good idea. If you are
worried about database performance, you should look into ActiveRecord
caching.

  1. You can serialize the entire object to the browser when the results
    are rendered and deserialize it in the controller when handling the
    click.

  2. You can store the results in the session and pull the object from the
    session by ID. You’ll need to handle the possibility that the session
    dies before the user clicks (e.g. if they go to lunch).

mike