model reference

hi

I am tanizawa

I am searching how to add reference of two same table.

Please teach me how to add .

exmple:

class CreateLocationjs < ActiveRecord::Migration

def change

create_table :locationjs do |t|

t.references :locationm <----start point

<— I want to add end point field.

Not sure what your exact question is, but this example below
references a location 2 times:

# Create models:
rails g model location name:string
rails g model route

# Add associations between models:
in Location, add:
     has_many :routes

in Route, add:
     belongs_to :location_start, :class_name => Location, :foreign_key
=> "location_start_id"
     belongs_to :location_end, :class_name => Location, :foreign_key
=> "location_end_id"

# Define foreign keys in db migration:

class CreateRoutes < ActiveRecord::Migration
  def change
    create_table :routes do |t|
      t.references :location_start
      t.references :location_end

      t.timestamps
    end
  end
end

# Now in console:
munich = Location.new
munich.name = "Munich"; munich.save!

tokyo = Location.new
tokyo.name = "Tokyo"; tokyo.save!

route = Route.new
route.location_start = munich
route.location_end = tokyo
route.save!

Thank you for your code.

I couldn’t have done it without you.
I understand how to add some reference for same table.