Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6935501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:06:40+00:00 2026-05-27T12:06:40+00:00

In rails when updating or creating, if i need to run another method this

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T12:06:41+00:00Added an answer on May 27, 2026 at 12:06 pm

    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:

    http://mysite.com/comments
    

    Which is then routed to the CommentsController#create action. Now, if you have a lot of code that goes into creating a new comment, then yes you should move some of that into the Comment model. Otherwise, if it’s pretty straightforward, don’t worry about it.

    Example of a straightforward scenario:

    # POST /comments
    def create
      @comment = Comment.new(:content => params[:comment_content])
    
      if @comment.save
        respond_with @comment
      else
        # error handling
      end
    end
    

    If you need to run a method after that comment is created, then put a callback in the Comment model:

    class Comment < ActiveRecord::Base
      after_create :do_something
    
      def do_something
        # some code here
      end
    end
    

    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 Comment model:

    CommentsController

    # POST /comments
    def create
      @comment = Comment.create_comment!(params[:comment_content])
    
      if @comment.errors.any?
        # handle errors
      else
        respond_with @comment
      end
    end
    

    Comment Model

    class << self
      def create_comment!(comment_content)
        comment = Comment.new(:content => comment_content)
    
        # lots of complex comment creation code goes here
    
        return comment
      end
    end
    

    This allows you to keep you controller code simple while moving more verbose code into your Model.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am updating an application to Rails 3 and I am having trouble creating
im trying to understand the process of creating tables in ruby-on-rails 3. i have
The specs for my rails project have been really slow lately. I did a
I don't have any experience in updating a Rails app and when I google
I see that some people are updating their apps to Rails 3.1. When is
I have the following for which allows for creating or updating a record: <%=form_for
I've recently installed Ruby on Rails 3.2 and have been trying to learn it.
Rails is a great platform, but it just doesn't have the history that Perl
Rails gurus: I've just discovered named_scope thanks to another SO user. :) I'd like
Rails has a nice idiom that makes it easy for you to have a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.