简体   繁体   中英

ROR - Two Field Relations Of One Model [ORM]

Case:
I have a model (" ride_request ")
in ride_request there are two fields ( pickup_location , dropoff_location )
both fields (pickup and dropoff) are instances of one model ( location )

Here is my migrate

create_table :ride_requests do |t|
  t.integer :pickup_location
  t.integer :dropoff_location

Question:
How do I create a relation to location in the fields pickup/drop when normaly you would use location_id?

you can use :class_name and :foreign_key to indicate the differences

Class RideRequest
  belongs_to :pickup, :class_name => "location", :foreign_key => "pickup_location"
  belongs_to :dropoff, :class_name => "location", :foreign_key => "dropoff_location"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM