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 8741135
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:15:23+00:00 2026-06-13T11:15:23+00:00

I’m new to ruby and rails and I’m wanting to following coding standards and

  • 0

I’m new to ruby and rails and I’m wanting to following coding standards and conventions as best as possible, so I don’t pick up any bad habits. I have two models: Course and Location. A course belongs_to a location, as a course can only have one location. A location has_many courses, as a location could be shared by more than one course.

When creating a course, a location may already exist that is found by its ID. Or the location may not yet exist, in which case a new location record must be created. My Course controller has the following create action.

def create
  @course = Course.new(params[:course])

  if params[:course][:location][:id].blank?
    @course.location = Location.create(params[:course][:location])
  else
    @course.location = Location.find_by_id(params[:course][:location][:id])
  end

  @course.save

  respond_with @course
end

Note that this is a REST API that only responds with JSON. The javascript that makes the request posts a JSON array in the same format that would be returned by a GET request

{
  "course":
  {
    "title":"US History",
    "credits":"3",
    "max_students":"100",
    "location":
    {
      "id":"",
      "building":"Freedom Hall",
      "room":"301"
    }
  }
}

or

{
  "course":
  {
    "title":"US History",
    "credits":"3",
    "max_students":"100",
    "location":
    {
      "id":"12", # this is the only difference
      "building":"Freedom Hall",
      "room":"301"
    }
  }
}
  1. Compared with all of the examples I’ve read, this code doesn’t look so elegant. Is there better way to factor it?
  2. If Location.create raises an exception, will @course.save still be called? Do I need to use Location.create!?
  3. Likewise, will validation errors end up in @course.errors even though the errors were on the Location model? Do I need to rescue from the exception so that I can return the errors to the client?

Thank you very much for any and all help!

  • 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-06-13T11:15:24+00:00Added an answer on June 13, 2026 at 11:15 am

    You can clean that up by using find_or_initialize_by_id. This should work:

    def create
      @course = Course.new(params[:course])
      @course.location = Location.find_or_initialize_by_id(params[:course][:location][:id],
                                                           params[:course][:location])
      @course.save
      respond_with @course
    end
    

    Regarding your second question, in your code as you have it @course.save will not be called if Location.create (or Location.find) raises an exception (because they would happen first). However, the way I have coded it above, the exception would happen at the same point in the code, when save is called, at which point the association is also saved.

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

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want use html5's new tag to play a wav file (currently only supported
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have a text area in my form which accepts all possible characters from
I don't have much knowledge about the IPv6 protocol, so sorry if the question
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

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.