Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to pass session variable to model in RoR?

I used a global variable in my app for passing information before. But I got a problem and thanks everyone here suggested me to store those data in session with database.

I tried, but I found that I can't access the session variable in Model. I googled and knew this is the Model normal behavior, RoR won't pass the session variable to Model.

So, I would like to use that session variable in validation and also the controller....

  1. how to pass the value of the session variable into Models? or

  2. is there any other method for my use case? I need a variable storing a value, which is required in all MVCs, and should be independent between different concurrent users.

Thanks everyone. :)

like image 889
Victor Lam Avatar asked Jun 10 '10 03:06

Victor Lam


4 Answers

If I understand you correctly, a session variable changes the way you validate the model. I believe the correct solution for this is the following:

class Blog < ActiveRecord::Base
  attr_accessor :validate_title

  validate_presence_of :title, :if => :validate_title
end

class BlogsController < ApplicationController
  def new
    @blog = Blog.new
    @blog.validate_title = session[:validate_title]
  end
end

The code has not been testet, but that's the idea. The if argument can be the name of a method and you can do whatever you want in there. You can have various validation modes if you want. For example:

class Blog < ActiveRecord::Base
  attr_accessor :validation_mode

  validate_presence_of :title, :if => :validate_title

  def validate_title
    validation_mode == "full" or validation_mode == "only_title"
  end
end

class BlogsController < ApplicationController
  def new
    @blog = Blog.new
    @blog.validate_mode = session[:validate_mode]
  end
end

For more information, read the guide on validation.

like image 88
pupeno Avatar answered Nov 15 '22 01:11

pupeno


Do you need the session variable as part of your model, or just as a flag to determine what to execute?

If the former, you don't need to know where did the parameters originate, just pass the variable as an argument for some call in your method. For example:

@user = User.new(params[:user].merge(:some_attribute => session[:some_key])

and just add the validation as usual in the model.

If you need that to control some flow of the execution, as you mention that should be different for different users, you may need an instance variable in your controller. Something like

class SomeController
  before_filter :set_some_session_variable

  def set_some_session_variable
    @some_variable = session[:some_key]
  end
end

You could use session[:some_key] directly in your view, but is better to set it in an instance variable instead.

like image 32
Chubas Avatar answered Nov 15 '22 03:11

Chubas


Best way of doing this is to Create a method with an argument and pass session as an argument.

Fro example

class User < ActiveRecord::Base

  def self.some_method(some_arg)
     User.find(some_arg)
  end

end

from controller

User.some_method(session[:user_id])
like image 1
Salil Avatar answered Nov 15 '22 01:11

Salil


Models are an interface to the database. They can be used without a session (e.g. from IRB). It would be a violation of MVC to allow the models to talk to the session. Can you expand your question with a bit more info about what you're trying to do? There is most probably a better way to do it.

like image 1
zaius Avatar answered Nov 15 '22 02:11

zaius