Arpit Jain
2009-May-24 07:54 UTC
How to pass arguments to functions in callbacks in model ?
I need to pass attributes to the function called in callback in rails
model. For example,
after_save :update_something
after_destroy :update_something, true #true is the argument that I need to
pass
private
def update_something(value = false)
#...
end
I found a way to do it like this
after_save :update_something
after_destroy {|record| record.update_something(true)}
But in this case, I need to make the function update_something public, which
I don''t want to do.
Any help ?
--
Arpit Jain
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---
Erdwin Lianata
2009-May-24 15:52 UTC
Re: How to pass arguments to functions in callbacks in model ?
What about ... def update_something(value=false) # .. end def after_save update_something(true) end Or use instance variable as conditional state instead of passing variable to a function called from macro (i.e after_save) Arpit Jain wrote:> I need to pass attributes to the function called in callback in rails > model. For example, > > after_save :update_something > after_destroy :update_something, true #true is the argument that I need > to pass > > private > def update_something(value = false) > #... > end > > > I found a way to do it like this > > after_save :update_something > after_destroy {|record| record.update_something(true)} > > But in this case, I need to make the function update_something public, > which I don''t want to do. > > Any help ? > > > -- > Arpit Jain
Samiron
2009-May-25 09:22 UTC
Re: How to pass arguments to functions in callbacks in model ?
You can use ActiveRecord::Observer. For details please go to
http://blog.invisible.ch/files/rails-reference-1.1.html
However theres another funny work around for this problem
as your desired syntax is
after_destroy :update_something, true
Just tweak little bit:
after_destroy "update_something(true)"
declare the update_something method privately like:
def update_something(param = value)
#operations based on #{param}
end
I dont know your actual scenario. But i think
* If u need to perform same action on :after_save and :after_destroy
call-back then why do you need parameter
* Secondly, if update_something performs differently based on the
parameter then why don''t you create two separate methods
Anyway looking for the real picture from you and hope to get the best
architectural suggestion from anyone for this kind of problem :-)
Thank you
Samiron Paul
http://samironpaul.blogspot.com
On May 24, 9:52 pm, Erdwin Lianata
<erdwin.lian...-/E1597aS9LS+ZvmcBgLQeg@public.gmane.org>
wrote:> What about ...
>
> def update_something(value=false)
> # ..
> end
>
> def after_save
> update_something(true)
> end
>
> Or use instance variable as conditional state instead of passing
> variable to a function called from macro (i.e after_save)
>
> Arpit Jain wrote:
> > I need to pass attributes to the function called in callback in rails
> > model. For example,
>
> > after_save :update_something
> > after_destroy :update_something, true #true is the argument that I
need
> > to pass
>
> > private
> > def update_something(value = false)
> > #...
> > end
>
> > I found a way to do it like this
>
> > after_save :update_something
> > after_destroy {|record| record.update_something(true)}
>
> > But in this case, I need to make the function update_something public,
> > which I don''t want to do.
>
> > Any help ?
>
> > --
> > Arpit Jain