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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:30:28+00:00 2026-05-23T00:30:28+00:00

I working in Rails 3 Activerecord. I have two models City and Shipment and

  • 0

I working in Rails 3 Activerecord. I have two models City and Shipment and I’m trying to model a shipment leaving from one city (ship_from) and shipping to another (ship_to). This is what I have:

class Shipment < ActiveRecord::Base
  belongs_to :ship_from, :class_name => "City", :foreign_key => "city_id"
  belongs_to :ship_to, :class_name => "City", :foreign_key => "city_id"
end

class City < ActiveRecord::Base
  has_many :ship_froms
  has_many :ship_tos
end

I know I missing something very simple and obvious, but I’m just not getting it. Any guidance would be greatly appreciated. Thanks.

  • 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-23T00:30:29+00:00Added an answer on May 23, 2026 at 12:30 am

    You’re modeling two one-to-many relations. You’re saying as Shipment has one from city and one to city.

    The one side is the part that’s saved in your table. So you’re saving a from_city_id and a to_city_id in your shipments table. Both these columns contain the id of a City.

    You model this one city relation by adding the following code in the Shipment class:

    belongs_to :from_city, :class_name => 'City'
    

    This code means we have a property from_city which type is City (from option :class_name, default would be FromCity). You can find the City by searching the table cities for id = from_city_id (id is the default for the option :primary_key, :from_city_id is the default for :foreign_key).

    For the to_city you declare

    belongs_to :to_city, :class_name => ‘City’

    The many part of the relation is in the City class. To say you have many Shipments from this city. You declare the following:

    many :from_shipments, :class_name => 'Shipment', :foreign_key => 'from_city_id'
    

    The code means we have a property from_shipments which type is a collection of Shipments (:class_name). You can find the Shipment by searching in the table shipments for from_city_id = id (we override the default city_id by declaring a :foreign_key)

    For the to_city you declare

    many :to_shipments, :class_name => 'Shipment', :foreign_key => 'to_city_id'
    

    More info about the defaults and options for belongs_to, has_one, has_many can be found here.

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

Sidebar

Related Questions

I'm working with an older version of Rails and ActiveRecord that does not have
I'm working with sqlalchemy, and coming from learning an orm with ruby/rails activerecord and
I'm working with Rails 3.2.2. And I have a devise model called user. In
In my rails application I have two models called Kases and Notes. They work
Lets say I have two Rails models, Users and Lists. Users have_many lists and
I currently have a Rails 3.0.10 app and I made two models with simple
I have two ActiveRecord models. Idea and User . There's an association between the
I am running Rails 3.0.3 with Paperclip 2.3.8. I have two models, call them
I am working on a rails 3 projects. One of my model is the
I am working on rails project and I am trying to get exceptions to

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.