Hi people I have a problem. I want to know if it is possible to call a method (in the controller of a class) from an action, a callback or something in a related model. e.g. Model A "has many" Model B But every time Model A is updated I need to execute a (custom) controller method in Model B Hope you can help me -- 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.
This is a bad idea. Models shouldn''t know about controllers at all. What does the controller action do? Perhaps you can just do that in a method on a model. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/dlO4UoqriGEJ. 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.
Well, the example I used is a little simple for what I really need. I have 4 models Model A, Model B, Model C, and Model D Everytime I update Model C, I have to update Model B Everytime I update Model D, I have to update Model B Everytime I update Model B, I have to update Model A, but this update depends on "which" model has updated Model B. if Model C has updated Model B I have to make some changes in Model A. But if Model D has updated Model B I have to make differnts changes in Model A. My problem is that I don''t know how can I recognize which model has updated Model B to "switch" between actions to update Model A On Aug 24, 2:50 pm, Tim Shaffer <timshaf...-BUHhN+a2lJ4@public.gmane.org> wrote:> This is a bad idea. Models shouldn''t know about controllers at all. > > What does the controller action do? Perhaps you can just do that in a method > on a model.-- 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.
Maybe you could use some sort of "updated by model" field on your Model B. In your controller (for example): model_c = ModelC.find model_c.save model_b = ModelB.find model_b.updated_by_model = ModelC Then in your model, you could do something like this: after_save :update_model_a def update_model_a model_a = ModelA.find model_a.foo = "bar" if updated_by_model = ModelC model_a.foo = "rab" if updated_by_model = ModelD end These are just very broad ideas since I don''t know the specifics. Hopefully they will spark some ideas for you, but it should be possible to do this in a more "railsy" way than calling a controller action from a model -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/PANQutQFe-4J. 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.
On Thu, Aug 25, 2011 at 2:20 AM, Angelo Cordova <acordinz-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Well, the example I used is a little simple for what I really need. > > I have 4 models > > Model A, Model B, Model C, and Model D > > Everytime I update Model C, I have to update Model B > Everytime I update Model D, I have to update Model B > > Everytime I update Model B, I have to update Model A, but this update > depends on "which" model has updated Model B. if Model C has updated > Model B I have to make some changes in Model A. But if Model D has > updated Model B I have to make differnts changes in Model A. > >I think the simplest way to do this is to have an after_save callback on ModelC and ModelD. Just call the method that will update ModelB and ModelA on the callbacks.> My problem is that I don''t know how can I recognize which model has > updated Model B to "switch" between actions to update Model A > > On Aug 24, 2:50 pm, Tim Shaffer <timshaf...-BUHhN+a2lJ4@public.gmane.org> wrote: > > This is a bad idea. Models shouldn''t know about controllers at all. > > > > What does the controller action do? Perhaps you can just do that in a > method > > on a model. > > -- > 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. > >-- ------------------------------------------------------------- visit my blog at http://jimlabs.heroku.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-/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.
Angelo Cordova wrote in post #1018303:> Well, the example I used is a little simple for what I really need. > > I have 4 models > > Model A, Model B, Model C, and Model D > > Everytime I update Model C, I have to update Model B > Everytime I update Model D, I have to update Model B > > Everytime I update Model B, I have to update Model A, but this update > depends on "which" model has updated Model B. if Model C has updated > Model B I have to make some changes in Model A. But if Model D has > updated Model B I have to make differnts changes in Model A. > > My problem is that I don''t know how can I recognize which model has > updated Model B to "switch" between actions to update Model ASounds like what you need is Key Value Observing (KVO). Too bad I know of nothing like that anywhere in Rails. Plus it probably doesn''t make sense in a web application anyway. http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/KeyValueObserving/KeyValueObserving.html Sorry, that wasn''t exactly helpful, but maybe an interesting... observation (pun intended). :) -- 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-/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.
Maybe you could use an Observer.... On Wed, Aug 24, 2011 at 10:10 PM, Robert Walker <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org>wrote:> Angelo Cordova wrote in post #1018303: > > Well, the example I used is a little simple for what I really need. > > > > I have 4 models > > > > Model A, Model B, Model C, and Model D > > > > Everytime I update Model C, I have to update Model B > > Everytime I update Model D, I have to update Model B > > > > Everytime I update Model B, I have to update Model A, but this update > > depends on "which" model has updated Model B. if Model C has updated > > Model B I have to make some changes in Model A. But if Model D has > > updated Model B I have to make differnts changes in Model A. > > > > My problem is that I don''t know how can I recognize which model has > > updated Model B to "switch" between actions to update Model A > > Sounds like what you need is Key Value Observing (KVO). Too bad I know > of nothing like that anywhere in Rails. Plus it probably doesn''t make > sense in a web application anyway. > > > http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/KeyValueObserving/KeyValueObserving.html > > Sorry, that wasn''t exactly helpful, but maybe an interesting... > observation (pun intended). :) > > -- > 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-/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. > >-- 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.
On Wed, Aug 24, 2011 at 11:20 AM, Angelo Cordova <acordinz-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Everytime I update Model B, I have to update Model A, but this update > depends on "which" model has updated Model B. if Model C has updated > Model B I have to make some changes in Model A. But if Model D has > updated Model B I have to make differnts changes in Model A.One Adam 12, code 7, Law of Demeter violation in progress :-) <http://en.wikipedia.org/wiki/Law_of_Demeter> You might want to rethink these models. Just sayin'' ... -- Hassan Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org http://about.me/hassanschroeder twitter: @hassan -- 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.
Thank all of you for your help... Finally I solve my problem by doing something really simple... I just made a subtraction and everything work just fine Thanks again -- 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.