In rails when updating or creating, if i need to run another method this is what i have been doing for my apps.(located in my controller)
def create
# Perform stuff
end
def update
# Perform stuff
end
Something about this looks like it is really hacked out, there must be a more professional way of doing this than what i have now. Is there some way i need to set up my model in order to run tasks when a model is created or updated.
In Rails, your controllers handle your incoming web requests. So if a person clicks a link to create a new comment, they might hit a route like:
Which is then routed to the
CommentsController#createaction. Now, if you have a lot of code that goes into creating a new comment, then yes you should move some of that into theCommentmodel. Otherwise, if it’s pretty straightforward, don’t worry about it.Example of a straightforward scenario:
If you need to run a method after that comment is created, then put a callback in the
Commentmodel:If it’s more complex to create a comment (i.e. if there is a ton of code that you don’t want in the Controller), then move some code into the
Commentmodel:CommentsController
Comment Model
This allows you to keep you controller code simple while moving more verbose code into your Model.