# Passing a model as a parameter

**URL:** <https://www.ruby-forum.com/t/passing-a-model-as-a-parameter/89354>\
**Category:** Rails\
**Created:** [March 19, 2007, 8:27pm UTC](https://www.ruby-forum.com/t/passing-a-model-as-a-parameter/89354 "2007-03-19T20:27:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![chris\_young](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/74df32/32.png) [@chris\_young](https://www.ruby-forum.com/u/chris_young)\
**Post date:** [March 19, 2007, 8:27pm UTC](https://www.ruby-forum.com/t/passing-a-model-as-a-parameter/89354/1 "2007-03-19T20:27:16Z")

</div>

Here’s the situation:

I need to do a redirect from a controller and I want to pass an object  
to the redirect but I can’t figure out how to pass it without it being  
turned into an unreadable string.

I’m doing this:

redirect\_to :controller =\> ‘pizza’, :task =\> params[:task]

but it is passing this in the request:

Parameters: {“task”=\>“titlesfdspriority1contentsdfuser\_id2”}

Any way I can get it back to looking like the original parameter:

“task”=\>{“title”=\>“fds”, “priority”=\>“1”, “content”=\>“d”,  
“user\_id”=\>“2”}

???

Thanks for your help

---

<div class="post-metadata">

**Author:** ![chris\_young](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/74df32/32.png) [@chris\_young](https://www.ruby-forum.com/u/chris_young)\
**Post date:** [March 19, 2007, 8:49pm UTC](https://www.ruby-forum.com/t/passing-a-model-as-a-parameter/89354/2 "2007-03-19T20:49:50Z")

</div>

chris young wrote:

> Any way I can get it back to looking like the original parameter:
> 
> “task”=\>{“title”=\>“fds”, “priority”=\>“1”, “content”=\>“d”,  
> “user\_id”=\>“2”}

How bout…

redirect\_to :controller =\> ‘pizza’, task[title] =\>  
params[:task][:title], task[priority] =\> params[:task][:priority], etc,  
etc

no clue if that’ll work, but try it and see.

Dave

---

<div class="post-metadata">

**Author:** ![chris\_young](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/74df32/32.png) [@chris\_young](https://www.ruby-forum.com/u/chris_young)\
**Post date:** [March 19, 2007, 8:52pm UTC](https://www.ruby-forum.com/t/passing-a-model-as-a-parameter/89354/3 "2007-03-19T20:52:35Z")

</div>

> redirect\_to :controller =\> ‘pizza’, :task =\> params[:task]
> 
> but it is passing this in the request:
> 
> Parameters: {“task”=\>“titlesfdspriority1contentsdfuser\_id2”}
> 
> Any way I can get it back to looking like the original parameter:

well… for only four parameters maybe you could just pass the four of  
them directly. If not another common approach is to pass just the id and  
hit the db again (if you are lucky the last find will be cached, so db  
will not be even hit).

anyway, for anytime you want to pass something like this you can always  
serialize. The ruby class for that is Marshal. Not total recall here,  
but I’d say i saw recently a ticket on the dev site about models not  
deserializing the relationships properly when using Marshal (just in  
case).

regards,

javier ramirez

## –

Estamos de estreno… si necesitas llevar el control de tus gastos  
visita [http://www.gastosgem.com](http://www.gastosgem.com) !!Es gratis!!

---

<div class="post-metadata">

**Author:** ![chris\_young](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/74df32/32.png) [@chris\_young](https://www.ruby-forum.com/u/chris_young)\
**Post date:** [March 19, 2007, 8:58pm UTC](https://www.ruby-forum.com/t/passing-a-model-as-a-parameter/89354/4 "2007-03-19T20:58:35Z")

</div>

On 3/19/07, javier ramirez [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> well… for only four parameters maybe you could just pass the four of  
> them directly. If not another common approach is to pass just the id and  
> hit the db again (if you are lucky the last find will be cached, so db  
> will not be even hit).
> 
> anyway, for anytime you want to pass something like this you can always  
> serialize. The ruby class for that is Marshal. Not total recall here,  
> but I’d say i saw recently a ticket on the dev site about models not  
> deserializing the relationships properly when using Marshal (just in case).

You could also store it in the session.

Isak

---

<div class="post-metadata">

**Author:** ![chris\_young](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/74df32/32.png) [@chris\_young](https://www.ruby-forum.com/u/chris_young)\
**Post date:** [March 19, 2007, 9:06pm UTC](https://www.ruby-forum.com/t/passing-a-model-as-a-parameter/89354/5 "2007-03-19T21:06:13Z")

</div>

Okay, I’m going to go with passing individual parameters. Man, I wish i  
hadn’t wasted an hour on this!

Thanks Javier and Isak!

javier ramirez wrote:

> > redirect\_to :controller =\> ‘pizza’, :task =\> params[:task]
> > 
> > but it is passing this in the request:
> > 
> > Parameters: {“task”=\>“titlesfdspriority1contentsdfuser\_id2”}
> > 
> > Any way I can get it back to looking like the original parameter:
> 
> well… for only four parameters maybe you could just pass the four of  
> them directly. If not another common approach is to pass just the id and  
> hit the db again (if you are lucky the last find will be cached, so db  
> will not be even hit).
> 
> anyway, for anytime you want to pass something like this you can always  
> serialize. The ruby class for that is Marshal. Not total recall here,  
> but I’d say i saw recently a ticket on the dev site about models not  
> deserializing the relationships properly when using Marshal (just in  
> case).
> 
> regards,
> 
> javier ramirez
> 
> ## –
> 
> Estamos de estreno… si necesitas llevar el control de tus gastos  
> visita [http://www.gastosgem.com](http://www.gastosgem.com) !!Es gratis!!
