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. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/vJDR-zjkuFcJ. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Mon, Jun 18, 2012 at 7:38 AM, tanizawa <kazuyat7175-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 Mulder <mulder.patrick-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>> On Mon, Jun 18, 2012 at 7:38 AM, tanizawa <kazuyat7175-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.