Forum: Ruby on Rails how to call a function in model and pass values in

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Fe31c86e61dfabddea38806d2731a9c1?d=identicon&s=25 jon (Guest)
on 2007-04-24 14:01

Is it possible to call a functon using callbacks, passing a number of
attr_names in?
i.e. somthing like this

before_save :tst => :title, :content

def test(*attr_names)
  attr_names.each do |record, attr_name, value|

B84d42a3a5c343f8fc6ab7d7f47fd3f5?d=identicon&s=25 Robby Russell (Guest)
on 2007-04-25 03:36
(Received via mailing list)
jon wrote:
>   end
> end

Two questions.

1) Are you trying to create a custom validation in your model? This
wouldn't be the route to take.

I'm going to guess that this is the case.

You'd want to extend AR to build your own validation method.


-- or --

2) Do you just need a custom before_save method to be called?

If this, then I'm not sure why you'd need to pass an arbitrary set of
methods into the function.

before_save :test

def test
   [:attr_1, :attr_2].each...


Good luck!


Robby Russell
21f7ed21f11a809050594c82eab11d67?d=identicon&s=25 Robert Walker (Guest)
on 2007-04-25 10:14
(Received via mailing list)
Maybe it would be better if you explain what you are trying to

The method before_save() gets called by the Rails system, which is not
going to pass in any arguments.  It doesn't work quite like before or
after filters in controllers.  I suppose you can do just about
anything you want inside a before_save() method (within reason and
possibly some constraints).

> before_save :tst => :title, :content
A before_save would look more like this:

def before_save
  # do what you want here
06db81f02537377c4bb207cdf8fd18ec?d=identicon&s=25 Pawel Rutkowski (Guest)
on 2007-04-28 17:14
>> before_save :tst => :title, :content
> A before_save would look more like this:
> def before_save
>   # do what you want here
> end

Accualy no.
Your version will overload before_save method. Using it by the way
mention by jon it will place tst method on stack to call before save.
You can use many before_save ie.:

class Transaction < ActiveRecord
  before_save :check_account
  before_save :get_some_money
  before_save :get_even_more_money

That code will invoke all above methods in placed order.
F8ad08fcc4037b9389d7008729f2fbcf?d=identicon&s=25 Chris (Guest)
on 2007-04-29 05:32
(Received via mailing list)
On Apr 24, 7:01 am, jon <> wrote:
>   end
> end
> --
> Posted via

Assuming what you're wanting to do is create a method that exists for
all AR descendants and can be set as a before_save hook whenever you
call something similar to 'before_save :test => :title, :content' then
you could always write your own class method like this:

<code language="ruby">
class ActiveRecord::Base
  class << self
    # This class method just defines a new aptly named instance method
    # that is then registered with before_save.  The new instance
    # is a simple delegate to the real Model#test method you already
    # defined and working.
    def test(*attrs)
      self.class_eval <<-EOC
        def before_save_test_#{attrs.join('_and_')}
        before_save :before_save_test_#{attrs.join('_and_')}

  # Here is the stub for your real Model#test method.  It will be
  # called with the exact list of arguments given to the
  # Model.test class method.
  def test(*attrs)
    puts "instance test called with #{attrs.inspect}"

And then use it like this:

<code language="ruby">
class Post < ActiveRecord::Base
  test :title, :content

Obviously, you'll need to ensure that the snippet above defining the
custom class method gets loaded after ActiveRecord::Base does or re-
factor it to ensure AR is loaded as a result of this snippet being
This topic is locked and can not be replied to.