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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Robert Walker
2007-Apr-24 13:51 UTC
Re: how to call a function in model and pass values in
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, :contentA before_save would look more like this: def before_save # do what you want here end On Apr 24, 8:01 am, jon <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> 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 > > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Robby Russell
2007-Apr-24 14:04 UTC
Re: how to call a function in model and pass values in
jon wrote:> 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 >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. * http://www.marklunds.com/articles/one/312 -- 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... end Good luck! Robby -- Robby Russell http://www.robbyonrails.com/ http://www.planetargon.com/ --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Pawel Rutkowski
2007-Apr-28 15:14 UTC
Re: how to call a function in model and pass values in
>> before_save :tst => :title, :content > A before_save would look more like this: > > def before_save > # do what you want here > endAccualy 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. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Apr 24, 7:01 am, jon <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> 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 > > -- > 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: <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 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 </code> And then use it like this: <code language="ruby"> class Post < ActiveRecord::Base test :title, :content end </code> 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. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---