How to call a function in model and pass values in

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

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 –

  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/

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

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.

On Apr 24, 7:01 am, jon [email protected] 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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs