# Get updated model values in Controller

**URL:** <https://www.ruby-forum.com/t/get-updated-model-values-in-controller/115075>\
**Category:** Rails\
**Created:** [September 20, 2007, 11:44am UTC](https://www.ruby-forum.com/t/get-updated-model-values-in-controller/115075 "2007-09-20T11:44:42Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![shishir\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f07891/32.png) [@shishir\_s](https://www.ruby-forum.com/u/shishir_s)\
**Post date:** [September 20, 2007, 11:44am UTC](https://www.ruby-forum.com/t/get-updated-model-values-in-controller/115075/1 "2007-09-20T11:44:42Z")

</div>

Hi All,

I have a model instance M1 , which has a dynamically changing instance  
variable say V. I want to access the value of V in my view through  
controller C using AJAX requests.

The problem is that for every new AJAX request a new controller  
instance gets created. This new controller instance does not have any  
reference to my already existing Model object M1 and therefore to the  
variable V.

I have tried using session[:M1] but I get server error when I run the  
page.

Can I not keep the model object instances in session ?? Can anyone  
point out if I am doing anything wrong, or maybe suggest an  
alternative solution.

TIA  
Shishir

---

<div class="post-metadata">

**Author:** ![shishir\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f07891/32.png) [@shishir\_s](https://www.ruby-forum.com/u/shishir_s)\
**Post date:** [September 20, 2007, 4:05pm UTC](https://www.ruby-forum.com/t/get-updated-model-values-in-controller/115075/2 "2007-09-20T16:05:03Z")

</div>

Shishir S. wrote:

> Hi All,
> 
> I have a model instance M1 , which has a dynamically changing instance  
> variable say V. I want to access the value of V in my view through  
> controller C using AJAX requests.
> 
> The problem is that for every new AJAX request a new controller  
> instance gets created. This new controller instance does not have any  
> reference to my already existing Model object M1 and therefore to the  
> variable V.
> 
> I have tried using session[:M1] but I get server error when I run the  
> page.
> 
> Can I not keep the model object instances in session ?? Can anyone  
> point out if I am doing anything wrong, or maybe suggest an  
> alternative solution.
> 
> TIA  
> Shishir

You should post some code on your controller and how you are persisting  
the info in the session.

You CAN store Models in the session, although it’s a bad practice from  
the point of view of ‘staleness’ of data in the session model compared  
to what’s potentially in the db.

Better to store ID’s and ‘find’ the info as you need it.

I await your code to help debug your problem.

thanks.

---

<div class="post-metadata">

**Author:** ![shishir\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f07891/32.png) [@shishir\_s](https://www.ruby-forum.com/u/shishir_s)\
**Post date:** [September 20, 2007, 5:59pm UTC](https://www.ruby-forum.com/t/get-updated-model-values-in-controller/115075/3 "2007-09-20T17:59:53Z")

</div>

Hi Jean,

You are right, the problem however is that my model object M1 is not  
serializable and hence cannot be dumped by the session. In fact I  
don’t want to store it even in a db since it’s a non -AR model.

Here’s my model code:  
class Login

def connect id, password  
@jid = id  
@password = password  
@cl = Jabber::Client::new(@jid)  
@cl.connect(host=‘[im.oracle.com](http://im.oracle.com)’, port=5223)  
@cl.auth\_nonsasl(@password, false)  
@cl.add\_message\_callback do |m|  
if m.type != :error  
assign\_message(m.body,m.from)  
if m.body == ‘exit’  
@cl.close  
end  
end  
end  
end

```
    def assign_message msg_b, msg_f
     @mesage =Jabber::Message::new(msg_f,

```

msg\_b).set\_type(:normal).set\_id(‘1’)  
end

```
    def throw
return @mesage.body

```

end  
end

Now this is basically a connection object which maintains my stream  
connection to a server.

Here’s the controller part.

class LoginController \< ApplicationController

model :Login

def index  
end

def sign\_in

@jid = Jabber::JID::new(@params[‘user\_name’]+‘/ruby’)  
@jid.domain=‘[oracle.com](http://oracle.com)’  
@password = params[‘pwd’]  
#Testing Login model creation  
@log\_in = Login.new  
@log\_in.connect(@jid,@password)  
session[:sess]= @log\_in  
session[:sess].connect

puts(‘signed in to oracle’)  
end

def throw\_msg  
render\_text “message received : #{session[:sess].throw}”  
end

end

This controller gets created everytime I send an AJAX request calling  
the throw\_msg method in the controller to receive the new messages  
coming in the model. But since the model object cannot be persisted it  
doesn’t know of the messages received.

Can you please suggest any alternative method.

~shishir

On Sep 20, 7:05 pm, Jean N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![shishir\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f07891/32.png) [@shishir\_s](https://www.ruby-forum.com/u/shishir_s)\
**Post date:** [September 21, 2007, 8:37am UTC](https://www.ruby-forum.com/t/get-updated-model-values-in-controller/115075/4 "2007-09-21T08:37:49Z")

</div>

> If you have no way to serialize, or even better persist, the info into a  
> db, then I think you are stuck maybe having to look at a daemon process  
> that handles the messaging and you can query THAT in with your call to  
> the “throw\_msg” method.

I am not really into threads and daemon processes. :(. What I am  
wondering here is that unlike java where I can maintain session for  
such objects why can i not do that on rails.

Or else, and I’m really just brainstorming here,

> what about a class variable that is shared across all instances of your  
> controller and have that route your messages?

Not possible, there are two problems with that approach.

First the shared class variable will not be able to maintain more than  
1 user session. ( Basically when say N users connect I need N  
instances of my model object.)  
Second the class variable will also get initialized every time the  
controller class get instantiated because I create the model object  
within the controller class.

Stuck badly.

☹  
~Shishir

---

<div class="post-metadata">

**Author:** ![shishir\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f07891/32.png) [@shishir\_s](https://www.ruby-forum.com/u/shishir_s)\
**Post date:** [September 21, 2007, 5:03pm UTC](https://www.ruby-forum.com/t/get-updated-model-values-in-controller/115075/5 "2007-09-21T17:03:10Z")

</div>

Shishir S. wrote:

> First the shared class variable will not be able to maintain more than  
> 1 user session. ( Basically when say N users connect I need N  
> instances of my model object.)  
> Second the class variable will also get initialized every time the  
> controller class get instantiated because I create the model object  
> within the controller class.

So create a hash of your models, username =\> model. Store the hash as a  
class variable and put the username in the session. Just remember that  
you’ll need to clean up the hash when the session dies. Also keep in  
mind that this does not scale beyond one Ruby process. If you want to  
scale, you’ll need to rethink your approach.

mike

---

<div class="post-metadata">

**Author:** ![shishir\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f07891/32.png) [@shishir\_s](https://www.ruby-forum.com/u/shishir_s)\
**Post date:** [September 21, 2007, 6:58pm UTC](https://www.ruby-forum.com/t/get-updated-model-values-in-controller/115075/6 "2007-09-21T18:58:16Z")

</div>

Shishir S. wrote:

> Now this is basically a connection object which maintains my stream  
> connection to a server.

Usually, when displaying results from a remote database on a webpage,  
you want to open and close the connection with each user request. You  
don’t store the connection in the session, you store the means to get  
the connection (eg. the user’s id and password).

---

<div class="post-metadata">

**Author:** ![shishir\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f07891/32.png) [@shishir\_s](https://www.ruby-forum.com/u/shishir_s)\
**Post date:** [September 20, 2007, 9:37pm UTC](https://www.ruby-forum.com/t/get-updated-model-values-in-controller/115075/7 "2007-09-20T21:37:51Z")

</div>

> This controller gets created everytime I send an AJAX request calling  
> the throw\_msg method in the controller to receive the new messages  
> coming in the model. But since the model object cannot be persisted it  
> doesn’t know of the messages received.
> 
> Can you please suggest any alternative method.
> 
> ~shishir
> 
> On Sep 20, 7:05 pm, Jean N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

If you have no way to serialize, or even better persist, the info into a  
db, then I think you are stuck maybe having to look at a daemon process  
that handles the messaging and you can query THAT in with your call to  
the “throw\_msg” method. Or else, and I’m really just brainstorming here,  
what about a class variable that is shared across all instances of your  
controller and have that route your messages?

Just a though.
