How to access params in the callback of a Rails model?

21,815

Solution 1

params are not accessible in models, even if you pass them as a parameter then it would be consider as bad practice and might also be dangerous.

What you can do is to create virtual attribute and use it in your model.

class User < ActiveRecord::Base
 attr_accessible :name, :sign_up_date
 has_many :enrollments

 after_create :create_enrollment_log
 private
 def create_enrollment_log
   enrollments.create!(status: 'signed up', started: sign_up_date)
 end
end

Where sign_up_date is your virtual attribute

Solution 2

params will not be available inside the models.

One possible way to do this would be to define a virtual attribute in the user model and use that in the callback

class User < ActiveRecord::Base
 attr_accessible :name,:sign_up_date
 has_many :enrollments

 after_create :create_enrollment_log
 private
 def create_enrollment_log
   enrollments.create!(status: 'signed up', started: sign_up_date)
 end
end

you will have to make sure that the name of the field in the form is user[:sign_up_date]

Share:
21,815
at.
Author by

at.

Updated on July 09, 2022

Comments

  • at.
    at. almost 2 years

    I have a callback of a model that needs to create a dependent object based on another field entered in the form. But params is undefined in the callback method. Is there another way to access it? What's the proper way to pass a callback method parameters from a form?

    class User < ActiveRecord::Base
      attr_accessible :name
      has_many :enrollments
    
      after_create :create_enrollment_log
      private
      def create_enrollment_log
        enrollments.create!(status: 'signed up', started: params[:sign_up_date])
      end
    end
    
  • at.
    at. over 11 years
    wouldn't this try to save it to the database as well, where the sign_up_date user field doesn't exist?
  • at.
    at. over 11 years
    This wouldn't conflict with the database?
  • kp2222
    kp2222 over 11 years
    nope, unless there is a matching column in the users table it can not save it to the database
  • Muhamamd Awais
    Muhamamd Awais over 11 years
    not at all unless you have an attribute of same name in database i would suggest you to have look at this rails cast railscasts.com/episodes/167-more-on-virtual-attributes
  • at.
    at. over 11 years
    I get the following error: undefined method sign_up_date' for #<User:0x007fcd17888bc8>` when trying to assign it. Which does seem odd as without querying the database, it looks just like the name field which I have no problem assigning. Does Rails query the database upon .new or from a date_select form helper?
  • at.
    at. over 11 years
    I get undefined method sign_up_date' for #<User:0x007fcd17888bc8>` when using date_select in my form. Or I get undefined method sign_up_date=' for #<User:0x007fcd124609d8>` if I try to do a simple assignment to it in my controller.
  • K M Rakibul Islam
    K M Rakibul Islam almost 10 years
    A virtual attribute shoud be 'attr_accessor' but not attr_accessible! These two things are totally different!