How to pass session variable to model in RoR?

I used a global variable in my application to pass information before. But I had a problem and thanks to everyone who suggested that I store this data in a session with the database.

I tried, but found that I cannot access the session variable in the model. I googled and knew that this is the normal behavior of the model, RoR will not pass the session variable to Model.

So, I would like to use this session variable in validation as well as the controller ....

  • How to pass the value of a session variable in a model? or

  • Is there any other method for my use case? I need variable value storage, which is required in all MVCs and must be independent between different concurrent users.

Thanks to everyone. :)

+4
source share
4 answers

If I understand you correctly, the session variable changes the way the model is validated. 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 was not a testet, but this idea. The if argument can be the name of a method, and you can do whatever you want. You can use various verification modes if you wish. For instance:

 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 

Read the verification guide for more information.

+6
source

Do you need a session variable as part of your model or as a flag to determine what needs to be done?

If the first, you don’t need to know where the parameters came from, just pass the variable as an argument for some call in your method. For instance:

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

and just add validation, as usual, to the model.

If you need to control some thread of execution, since you mention that it should be different for different users, you may need an instance variable in your controller. Sort of

 class SomeController before_filter :set_some_session_variable def set_some_session_variable @some_variable = session[:some_key] end end 

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

+2
source

The best way to do 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 the controller

 User.some_method(session[:user_id]) 
+1
source

Models are the database interface. They can be used without a session (for example, from the IRB). It would be a violation of MVC so that models can talk to the session. Can you expand your question a little more about what you are trying to do? This is most likely the best way to do this.

+1
source

Source: https://habr.com/ru/post/1312395/


All Articles