Get updated model values in Controller

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

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.

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’, 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
@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. [email protected]

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.

:frowning:
~Shishir

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

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).

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. [email protected]

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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs