Chris Bloom
2007-Nov-10 06:25 UTC
Where do I define a method so it''s available to all models?
I''m trying to define a method that will be available to all Models. I originally put it in the ApplicationHelper file, but I now understand that this is only available to Views. Where do I put a common model method then? PS: If it makes a difference, the method needs to be able to reference the calling model, i.e. passing the model instance as an argument -- 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 -~----------~----~----~----~------~----~------~--~---
Raecoo
2007-Nov-10 10:20 UTC
Re: Where do I define a method so it''s available to all models?
you can create a module mix you application_controller On 11月10日, 下午2时25分, Chris Bloom <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> I''m trying to define a method that will be available to all Models. I > originally put it in the ApplicationHelper file, but I now understand > that this is only available to Views. Where do I put a common model > method then? > > PS: If it makes a difference, the method needs to be able to reference > the calling model, i.e. passing the model instance as an argument > -- > 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Raecoo
2007-Nov-10 10:21 UTC
Re: Where do I define a method so it''s available to all models?
type error, must mix you model On 11月10日, 下午6时20分, Raecoo <rae...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> you can create a module mix you application_controller > > On 11月10日, 下午2时25分, Chris Bloom <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > wrote: > > > I''m trying to define a method that will be available to all Models. I > > originally put it in the ApplicationHelper file, but I now understand > > that this is only available to Views. Where do I put a common model > > method then? > > > PS: If it makes a difference, the method needs to be able to reference > > the calling model, i.e. passing the model instance as an argument > > -- > > 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Chris Bloom
2007-Nov-10 14:05 UTC
Re: Where do I define a method so it''s available to all mode
Raecoo Cao wrote:> you can create a module mix you application_controller > type error, must mix you model > > On 11��10��, ����2ʱ25��, Chris Bloom <rails-mailing-l...@andreas-s.net>I''m sorry, I don''t understand. Do you mean that I can add it to my Application controller? According to the comments in that file, anything added will be available to all controllers, but it doesn''t mention models. I need it to be available to models, including when run from the console. -- 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
WadeWinningham
2007-Nov-10 15:13 UTC
Re: Where do I define a method so it''s available to all models?
You can write an extension to put in the lib/ directory and include it in all of your models... module MyExtension def added_function "stuff" end end ------- class MyModel < ActiveRecord::Base include MyExtension end Or you could create a new base class that inherits from ActiveRecord::Base and have your models inherit from your new base class. On Nov 10, 12:25 am, Chris Bloom <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> I''m trying to define a method that will be available to all Models. I > originally put it in the ApplicationHelper file, but I now understand > that this is only available to Views. Where do I put a common model > method then? > > PS: If it makes a difference, the method needs to be able to reference > the calling model, i.e. passing the model instance as an argument > -- > 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 -~----------~----~----~----~------~----~------~--~---
Chris Bloom
2007-Nov-10 15:24 UTC
Re: Where do I define a method so it''s available to all mode
WadeWinningham wrote:> You can write an extension to put in the lib/ directory and include it > in all of your models... > > module MyExtension > def added_function > "stuff" > end > endAh, yes, OK. I figured it was something like that, but I just didn''t know where to put it.> > ------- > > class MyModel < ActiveRecord::Base > include MyExtension > end > > Or you could create a new base class that inherits from > ActiveRecord::Base and have your models inherit from your new base > class.That makes sense too. Perhaps if I was adding more than one method. I''ll go the route of the lib file and just require it. Thanks!! -- 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 -~----------~----~----~----~------~----~------~--~---
Raecoo
2007-Nov-11 01:54 UTC
Re: Where do I define a method so it''s available to all mode
WadeWinningham is right --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Chris Bloom
2007-Nov-11 21:41 UTC
Re: Where do I define a method so it''s available to all mode
WadeWinningham wrote:> Or you could create a new base class that inherits from > ActiveRecord::Base and have your models inherit from your new base > class.If I were to go this route and create a new base class, do i put it in it''s own file in the model folder, and then do I require that file in each related model, or in the application controller, or somewhere else? -- 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 -~----------~----~----~----~------~----~------~--~---
Mark J
2007-Nov-12 04:22 UTC
Re: Where do I define a method so it''s available to all mode
Yes, you can put it in the model directory, mark it as an abstract model class, and make sure it won''t collide with any other model. You don''t have to explicitly call require in other models, just specify it as the parent class, and Rails with automatically load the specific file. Make sure to follow the Class name -> filename convention, so Rails can find it. Have Rails autoload it, so it can reload the file when needed in development mode. Thanks! Mark http://www.simpleteq.com On Nov 12, 5:41 am, Chris Bloom <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> WadeWinningham wrote: > > Or you could create a new base class that inherits from > > ActiveRecord::Base and have your models inherit from your new base > > class. > > If I were to go this route and create a new base class, do i put it in > it''s own file in the model folder, and then do I require that file in > each related model, or in the application controller, or somewhere else? > -- > 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 -~----------~----~----~----~------~----~------~--~---