I put some code in a before_save callback in the model file to alter some fields that have dependencies in the DB. I''d like to call a method in the controller file but I continually get undefined method errors. I have specified Controller::methodname as well to no avail. What am I missing? -- Posted via http://www.ruby-forum.com/.
Say you have your object is my_obj. Then do my_obj.before_save -- Posted via http://www.ruby-forum.com/.
Maybe I wasn''t clear. I have a routine I''d like to invoke FROM the before_save callback. I''d like to place the routine in the controller file rather than in the model. When I move it I continually get undefined method. Sorry for the confusion. -- Posted via http://www.ruby-forum.com/.
Mike Kogan wrote:> Maybe I wasn''t clear. I have a routine I''d like to invoke FROM the > before_save callback. > > I''d like to place the routine in the controller file rather than in the > model. > > When I move it I continually get undefined method. Sorry for the > confusion.Why do you think it belongs in a controller? Once the flow of control has entered the model, you shouldn''t be trying to jump back up into the controller layer. Technically, you migt be able to solve your problem by using a class method on the controller. Architecturally, this probably wouldn''t be the right thing to do. regards Justin
Justin Forder wrote:> Mike Kogan wrote: >> Maybe I wasn''t clear. I have a routine I''d like to invoke FROM the >> before_save callback. >> >> I''d like to place the routine in the controller file rather than in >> the model. >> >> When I move it I continually get undefined method. Sorry for the >> confusion. > > Why do you think it belongs in a controller? Once the flow of control > has entered the model, you shouldn''t be trying to jump back up into the > controller layer. > > Technically, you migt be able to solve your problem by using a class^^^^ might> method on the controller. Architecturally, this probably wouldn''t be the > right thing to do. > > regards > > Justin > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > >
Mike, You should never invoke a controller method from a model directly. You can''t assume that a controller exists when your model is called. Perhaps if you explain a little more about what you''re trying to achieve, someone can suggest an appropriate solution. Pete Yandell http://9cays.com/ On 10/07/2006, at 3:05 AM, Mike Kogan wrote:> Maybe I wasn''t clear. I have a routine I''d like to invoke FROM the > before_save callback. > > I''d like to place the routine in the controller file rather than in > the > model. > > When I move it I continually get undefined method. Sorry for the > confusion. > > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Thank you Peter. Let me give you a couple of scenarios. I was also just reflecting what someone advised me to do in another thread -- perhaps it wasn''t the best advice. I have a little fishing tournament app with a Catch table where all the catches go. Each catch has a length, from which points are determined. Depending on the bait (live, artificial, fly) we give a points adjustment (eg: 10% for lures, 20% for using fly tackle). So I was trying to update the points field from the size input by the user. Initially I was doing this in the Catch controller both in the new catch method and the update/edit method, and I had a bug due to the fact that I was referencing the old points in the DB record which hadn''t been committed yet. I got some advice to try the bfore_save callback and while that worked, the advisor also said it would be more "proper" MVC-wise to put the actual code in the controller and invoke it from the model. Thus, I got into this connundrum of trying to figure out how to make that work and got into undefined method issues. As far as syntax styles, deprecated APIs etc, what bites is that EVERY tutorial and example on RoR uses the "old style" APIs such as find_all etc rather than find (:all), so there is quite a limited # of examples using the "proper" API (some Ruby but not RoR). I find it particularly lacking that the API reference does not show more examples of the syntax in use. It''s not productive having to learn the same thing 2x, 3x. Now I have to go recode everything to the proper API ;) -- Posted via http://www.ruby-forum.com/.
Mike Kogan wrote:> Thank you Peter. Let me give you a couple of scenarios. I was also just > reflecting what someone advised me to do in another thread -- perhaps it > wasn''t the best advice. > > I have a little fishing tournament app with a Catch table where all the > catches go. Each catch has a length, from which points are determined. > Depending on the bait (live, artificial, fly) we give a points > adjustment (eg: 10% for lures, 20% for using fly tackle). So I was > trying to update the points field from the size input by the user. > Initially I was doing this in the Catch controller both in the new catch > method and the update/edit method, and I had a bug due to the fact that > I was referencing the old points in the DB record which hadn''t been > committed yet. I got some advice to try the bfore_save callback and > while that worked, the advisor also said it would be more "proper" > MVC-wise to put the actual code in the controller and invoke it from the > model. Thus, I got into this connundrum of trying to figure out how to > make that work and got into undefined method issues.I think if you re-read that advice you will see that the adviser (Chris Carter) said "you really should put that calculation in the model, not in the controller, then call it from the controller. This will keep your code clean and DRY, and makes it more MVCish/Railsish.".> > As far as syntax styles, deprecated APIs etc, what bites is that EVERY > tutorial and example on RoR uses the "old style" APIs such as find_all > etc rather than find (:all), so there is quite a limited # of examples > using the "proper" API (some Ruby but not RoR). I find it particularly > lacking that the API reference does not show more examples of the syntax > in use. It''s not productive having to learn the same thing 2x, 3x. Now I > have to go recode everything to the proper API ;)It is a problem that Rails evolves very fast, leaving the tutorials beind. The API documentation gives a reasonable set of examples for find: http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M000860 regards Justin
Oops, guess I got it backwards. Good, then I am doing it right! -- Posted via http://www.ruby-forum.com/.