I have a model User that has property called status. Status is just a string that can only be evaluated and not persisted. I.e. it doesn''t have a field in the user table. Anyways, I would like a method get_status to be called anytime I create a new instance of User. For example: user=User.new Since I created a new instance of User, I should now have access to the status property, e.g.: user.status =>"Ready" Here is my User class: class User < ActiveRecord::Base has_and_belongs_to_many :roles def evaluate_status #evaluate the status #@status = ... end end How would I call get_status whenever a new instance of User is created? It''s constructor? Thanks --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
initialize or after_initialize should do what you need. Initialize is called by ruby when the object is, well, initialized... cheers, Dan ================================class User < ActiveRecord::Base has_and_belongs_to_many :roles def initialize evaluate_status end def evaluate_status #evaluate the status #@status = ... end end ================================ On Aug 24, 9:15 am, eggie5 <egg...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I have a model User that has property called status. Status is just a > string that can only be evaluated and not persisted. I.e. it doesn''t > have a field in the user table. > > Anyways, I would like a method get_status to be called anytime I > create a new instance of User. For example: > > user=User.new > > Since I created a new instance of User, I should now have access to > the status property, e.g.: > > user.status > =>"Ready" > > Here is my User class: > > class User < ActiveRecord::Base > has_and_belongs_to_many :roles > > def evaluate_status > #evaluate the status > #@status = ... > end > end > > How would I call get_status whenever a new instance of User is > created? It''s constructor? > > Thanks--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
It doesn''t seem like the initializer gets called when you create a new instance using .find(), e.g.: c=ContentCampaign.find(1) c.status => nill However, If I create a new instance using new it works, e.g: d=ContentCampaign.new c.status => Waiting Here is my class, what''s going on? class ContentCampaign < ActiveRecord::Base belongs_to :category, :class_name => "Category", :foreign_key => "category_id" def initialize @status= evaluate_status end def evaluate_status ''Waiting'' end def status @status end end On Aug 23, 5:18 pm, dang <dgr...-vQs5aMGR+7nQT0dZR+AlfA@public.gmane.org> wrote:> initialize or after_initialize should do what you need. Initialize is > called by ruby when the object is, well, initialized... > > cheers, > Dan > > ================================> class User < ActiveRecord::Base > has_and_belongs_to_many :roles > > def initialize > evaluate_status > end > > def evaluate_status > #evaluate the status > #@status = ... > end > end > ================================> > On Aug 24, 9:15 am, eggie5 <egg...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > I have a model User that has property called status. Status is just a > > string that can only be evaluated and not persisted. I.e. it doesn''t > > have a field in the user table. > > > Anyways, I would like a method get_status to be called anytime I > > create a new instance of User. For example: > > > user=User.new > > > Since I created a new instance of User, I should now have access to > > the status property, e.g.: > > > user.status > > =>"Ready" > > > Here is my User class: > > > class User < ActiveRecord::Base > > has_and_belongs_to_many :roles > > > def evaluate_status > > #evaluate the status > > #@status = ... > > end > > end > > > How would I call get_status whenever a new instance of User is > > created? It''s constructor? > > > Thanks--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
eggie5 wrote:> It doesn''t seem like the initializer gets called when you create a new > instance using .find(), e.g.: > > c=ContentCampaign.find(1) > c.status > > => nill > > However, If I create a new instance using new it works, e.g: > > d=ContentCampaign.new > c.status > => Waiting > > Here is my class, what''s going on? > > class ContentCampaign < ActiveRecord::Base > belongs_to :category, :class_name => "Category", :foreign_key => > "category_id" > > def initialize > @status= evaluate_status > end > > > def evaluate_status > ''Waiting'' > end > > def status > @status > end > > endTry instead: class ContentCampaign < ActiveRecord::Base belongs_to :category, :class_name => "Category", :foreign_key =>"category_id" def evaluate_status ''Waiting'' end def status @status ||= evaluate_status end end --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
THANKS! that''s the trick I was looking for. Now this reminds me of an example from the essential rails book... So, it looks like status is called automatically when a new instance of the class is created? On Aug 24, 5:37 am, Sava Chankov <sava.chan...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> eggie5 wrote: > > It doesn''t seem like the initializer gets called when you create a new > > instance using .find(), e.g.: > > > c=ContentCampaign.find(1) > > c.status > > > => nill > > > However, If I create a new instance using new it works, e.g: > > > d=ContentCampaign.new > > c.status > > => Waiting > > > Here is my class, what''s going on? > > > class ContentCampaign < ActiveRecord::Base > > belongs_to :category, :class_name => "Category", :foreign_key => > > "category_id" > > > def initialize > > @status= evaluate_status > > end > > > def evaluate_status > > ''Waiting'' > > end > > > def status > > @status > > end > > > end > > Try instead: > > class ContentCampaign < ActiveRecord::Base > belongs_to :category, :class_name => "Category", :foreign_key > =>"category_id" > > def evaluate_status > ''Waiting'' > end > > def status > @status ||= evaluate_status > end > end--~--~---------~--~----~------------~-------~--~----~ 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 8/24/07, eggie5 <eggie5-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > THANKS! that''s the trick I was looking for. Now this reminds me of an > example from the essential rails book... > > So, it looks like status is called automatically when a new instance > of the class is created?does defining an after_initialize method not work for you? by the way, your belongs_to method doesn''t need the :class_name or :foreign_key specifications.. These can be inferred from the association itself. Adam --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
sorry I didn''t see you solution till after I tried accessor method. Regarding the foreign_key and class_name things, rails was warning me that if I could, I should specify them to avoid confusion. Since it was not big deal I just put them in. It''s the least I could do for ActiveRecord! On Aug 24, 12:09 pm, "Adam Cohen" <bionicboo...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 8/24/07, eggie5 <egg...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > THANKS! that''s the trick I was looking for. Now this reminds me of an > > example from the essential rails book... > > > So, it looks like status is called automatically when a new instance > > of the class is created? > > does defining an after_initialize method not work for you? > > by the way, your belongs_to method doesn''t need the :class_name or > :foreign_key specifications.. These can be inferred from the > association itself. > > Adam--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---