June 18, 2012, 7:39am
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
On Mon, Jun 18, 2012 at 7:38 AM, tanizawa
wrote: [email protected]
<--- I want to add end point
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
=> “location_end_id” Define foreign keys in db migration:
class CreateRoutes < ActiveRecord::Migration
create_table :routes do |t|
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
Thank you for your code.
I couldn’t have done it without you.
I understand how to add some reference for same table.
2012/6/18 Patrick M.