views:

77

answers:

4

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

+1  A: 

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])
Salil
thanks buddy. but i need that session variable in a validation. do you have any suggestion?
siulamvictor
A: 

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.

zaius
Hi zaius. I need this because I have a flag which will be changed in controllers according to different situation, and the Model will need this flag for choose the correct validation rules. Do you have any suggestion on this purpose? Thanks. :)
siulamvictor
+1  A: 

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.

Chubas
Hi Chubas. Thanks for your reply. Yes, it is a flag. I will try your suggested solution.
siulamvictor
+1  A: 

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.

J. Pablo Fernández
Thanks so much buddy~ It works well for me!
siulamvictor