Phoenix Rising
2010-Apr-29 22:21 UTC
Setting a message on a model -without- errors.add_to_base
I''m trying my best to follow the skinny controller/fat model concept, and I''ve got a series of callbacks (that I''m about to move into observers) running on a few models. Based on what these callbacks do to the data (it''s implementing business rules), I''d like to add some type of message on the state of the model *without* invalidating it. For example: class Order < ActiveRecord::Base after_save :do_something_cool private def do_something_cool # manipulates the order object in some way based on business rules errors.add_to_base("Message about what we did to your order to match said business rules") end end I''d prefer to do this without calling errors.add_to_base, because it isn''t an error. Furthermore, I don''t even think that works because it''s being called -after- the model is saved (so far, even though the callback in my case is running, it doesn''t appear to be adding anything to the base errors object anyway). Obviously I can''t set flash[:notice] inside the model as it''s owned by the controller, and I really don''t want to do some insane hacking to incorporate elements of the controller into the model (that would really make this spaghetti code and kill performance!). The only thing I''ve thought about so far is setting a virtual attribute of some kind, but I''d like to see if anybody has a better idea before I jump straight to that. Any thoughts on the matter? Thanks for any input you can provide! -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Sharagoz
2010-Apr-30 09:37 UTC
Re: Setting a message on a model -without- errors.add_to_base
On Apr 30, 12:21 am, Phoenix Rising <polarisris...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> The only thing I''ve thought about so far is setting a virtual > attribute of some kind, but I''d like to see if anybody has a better > idea before I jump straight to that.I think a non-DB ("virtual") attribute is a good idea. Simply declare the attributes with attr_accessor. class Order < AR::B attr_accessor :my_attribute end This will add the initialization of the instance variable and create getters and setters. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.