On Mon, Jun 18, 2012 at 7:38 AM, tanizawa <[email protected]> wrote:
> 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!

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to