Rails pass params/arguments to ActiveRecord callback function
There are four types of callbacks accepted by the callback macros: Method references (symbol), callback objects, inline methods (using a proc), and inline eval methods (using a string).
Try this?
after_update -> { cache_bust(true) }
Pass custom argument to rails event model callbacks?
No, it is not possible to pass variables to callback method. Reason is simple as callback methods are called by rails stack and when it will call callback method, it did't know what to pass in callback method.
Pass parameter to callback in Rails
You have:
belongs_to :user, foreign_key: :user_id
in your Favorite
model and limit_records
is an instance method on Favorite
. So you have access to the user as self.user_id
(or just user_id
since self
is implied) inside limit_records
and there is no need for an argument:
after_create :limit_records
def limit_records
# same as what you have now, `user_id` will be `self.user_id`
# now that there is no `user_id` argument...
count = self.where(user_id: user_id).count
self.where(used_id: user_id).last.delete if count > 10
end
Pass parameter to callback in the controller
To pass in parameters, you have to use a block.
before_filter(only: [:show]) { permit_show(@project) }
before_filter(only: [:edit]) { permit_edit(@project) }
How to pass params into a model callback method in rails?
I'm not an experienced railer, but I think it would work if you simply use a property of your model to store this value.
something like:
my_class.member = 'a-member'
class MyClass
after_update :reprocess_image
def reprocess_image
do_something_with_your self.member
end
end
Rails: Can you pass arguments to the after_create method?
You could use attr_accessor
to create a virtual attribute and set that attribute as part of your create action.
Rails Model callback: Passing field as parameter to callback classes?
You may do this or use normalize_attributes gem
module StripFieldsCallback
def before_validation_z(field)
write_attribute(field, read_attribute(field).strip) if read_attribute(field)
end
end
class User < ActiveRecord::Base
include StripFieldsCallback
before_validation lambda{|data| data.before_validation_z(:name)}
end
pass parameters to before_create callback in rails 3
I think you can store the message and the receivers of the message with the help of Ajax in the database. That way you can continue with the message after wards similar to gmail.
Another option is to have 2 different submits for "Send" and "Save as Draft" on the form and based on commit parameter you can process the message.
Related Topics
"Use" Keyword/Word in Ruby/Rails/Rack Code
Omniauth Facebook Expired Token Error
Ruby on Rails Model Inside Namespace Can't Be Found in Controller
How to Print Information About a Net:Httprequest for Debug Purposes
Building Hash by Grouping Array of Objects Based on a Property of the Items
Controlling Tor Client with Ruby
Convert HTML to Plain Text (With Inclusion of <Br>S)
Convert a Partial to Method/Block for Speed
What Rails Plugins Are Good, Stable and *Really* Enhance Your Code
Problem When Installing Ruby 2.2.9 on MAC Big Sur M1
Checking If a String Is Valid JSON Before Trying to Parse It
C1 or C2 Coverage Tool for Ruby
Including a Virtual Attribute in the Respond_With Hash
Where to Insert Rack::Deflater in the Rack