# How to call a function in model and pass values in

**URL:** <https://www.ruby-forum.com/t/how-to-call-a-function-in-model-and-pass-values-in/92810>\
**Category:** Rails\
**Created:** [April 24, 2007, 2:01pm UTC](https://www.ruby-forum.com/t/how-to-call-a-function-in-model-and-pass-values-in/92810 "2007-04-24T14:01:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3bc359/32.png) [@jon](https://www.ruby-forum.com/u/jon)\
**Post date:** [April 24, 2007, 2:01pm UTC](https://www.ruby-forum.com/t/how-to-call-a-function-in-model-and-pass-values-in/92810/1 "2007-04-24T14:01:05Z")

</div>

Hi,

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|

end  
end

---

<div class="post-metadata">

**Author:** ![jon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3bc359/32.png) [@jon](https://www.ruby-forum.com/u/jon)\
**Post date:** [April 25, 2007, 3:36am UTC](https://www.ruby-forum.com/t/how-to-call-a-function-in-model-and-pass-values-in/92810/2 "2007-04-25T03:36:26Z")

</div>

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.

- [Peter Marklund's Home](http://www.marklunds.com/articles/one/312)

– or –

1. 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…

end

Good luck!

Robby

–  
Robby R.  
[http://www.robbyonrails.com/](http://www.robbyonrails.com/)

> **[Planet Argon](https://www.planetargon.com/)**
>
> Planet Argon is a Ruby on Rails development agency specializing in maintenance and support for existing Rails apps. Based in Portland, OR, Planet Argon has been a member of the Rails community since 2004.

---

<div class="post-metadata">

**Author:** ![jon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3bc359/32.png) [@jon](https://www.ruby-forum.com/u/jon)\
**Post date:** [April 25, 2007, 10:14am UTC](https://www.ruby-forum.com/t/how-to-call-a-function-in-model-and-pass-values-in/92810/3 "2007-04-25T10:14:08Z")

</div>

Maybe it would be better if you explain what you are trying to  
accomplish.

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

end

---

<div class="post-metadata">

**Author:** ![jon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3bc359/32.png) [@jon](https://www.ruby-forum.com/u/jon)\
**Post date:** [April 28, 2007, 5:14pm UTC](https://www.ruby-forum.com/t/how-to-call-a-function-in-model-and-pass-values-in/92810/4 "2007-04-28T17:14:25Z")

</div>

> > 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  
end

That code will invoke all above methods in placed order.

---

<div class="post-metadata">

**Author:** ![jon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3bc359/32.png) [@jon](https://www.ruby-forum.com/u/jon)\
**Post date:** [April 29, 2007, 5:32am UTC](https://www.ruby-forum.com/t/how-to-call-a-function-in-model-and-pass-values-in/92810/5 "2007-04-29T05:32:13Z")

</div>

On Apr 24, 7:01 am, jon [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> end  
> end
> 
> –  
> Posted viahttp://www.ruby-forum.com/.

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:

`
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
method
    # is a simple delegate to the real Model#test method you already
have
    # defined and working.
    def test(*attrs)
      self.class_eval <<-EOC
        def before_save_test_#{attrs.join('_and_')}
          self.test(*#{attrs.inspect})
        end
        before_save :before_save_test_#{attrs.join('_and_')}
      EOC
    end
  end

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}”
end
end
`

And then use it like this:

`
class Post < ActiveRecord::Base
  test :title, :content
end
`

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  
evaluated.
