I am tanizawa
I am searching how to add reference of two same table.
Please teach me how to add .
class CreateLocationjs < ActiveRecord::Migration
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:
in Route, add:
belongs_to :location_start, :class_name => Location, :foreign_key
belongs_to :location_end, :class_name => Location, :foreign_key
# Define foreign keys in db migration:
class CreateRoutes < ActiveRecord::Migration
create_table :routes do |t|
# 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
Thank you for your code.
I couldn’t have done it without you.
I understand how to add some reference for same table.