Created
November 14, 2012 11:22
-
-
Save JeanMertz/4071617 to your computer and use it in GitHub Desktop.
route has_many locations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Route < ActiveRecord::Base | |
| belongs_to :to_location, class_name: 'Location' | |
| belongs_to :from_location, class_name: 'Location' | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Location < ActiveRecord::Base | |
| has_many :routes | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ActiveRecord::Schema.define(:version => 20121114100956) do | |
| create_table "locations", :force => true do |t| | |
| t.string "name" | |
| t.datetime "created_at", :null => false | |
| t.datetime "updated_at", :null => false | |
| end | |
| create_table "routes", :force => true do |t| | |
| t.string "name" | |
| t.integer "from_location_id" | |
| t.integer "to_location_id" | |
| t.datetime "created_at", :null => false | |
| t.datetime "updated_at", :null => false | |
| end | |
| add_index "routes", ["from_location_id"], :name => "index_routes_on_from_location_id", :unique => true | |
| add_index "routes", ["to_location_id"], :name => "index_routes_on_to_location_id", :unique => true | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # maak locatie "Maastricht" aan | |
| irb(main):001:0> Location.create name: 'Maastricht' | |
| => #<Location id: 1, name: "Maastricht", created_at: "2012-11-14 10:47:46", updated_at: "2012-11-14 10:47:46"> | |
| # maak locatie "Weert" aan | |
| irb(main):002:0> Location.create name: 'Weert' | |
| => #<Location id: 2, name: "Weert", created_at: "2012-11-14 10:47:54", updated_at: "2012-11-14 10:47:54"> | |
| # maak nieuwe route "thuis-werk" aan | |
| irb(main):003:0> route = Route.new(name: 'thuis-werk') | |
| => #<Route id: nil, name: "thuis-werk", from_location_id: nil, to_location_id: nil, created_at: nil, updated_at: nil> | |
| # voeg startlocatie "Maastricht" toe aan route "thuis-werk" | |
| irb(main):004:0> route.from_location = Location.find_by_name('Maastricht') | |
| => #<Location id: 1, name: "Maastricht", created_at: "2012-11-14 10:47:46", updated_at: "2012-11-14 10:47:46"> | |
| # voeg eindlocatie "Weert" toe aan route "thuis-werk" | |
| irb(main):005:0> route.to_location = Location.find_by_name('Weert') | |
| => #<Location id: 2, name: "Weert", created_at: "2012-11-14 10:47:54", updated_at: "2012-11-14 10:47:54"> | |
| # sla gegevens op | |
| irb(main):006:0> route.save | |
| SQL (0.8ms) INSERT INTO "routes" ("created_at", "from_location_id", "name", "to_location_id", "updated_at") | |
| VALUES (?, ?, ?, ?, ?) [["created_at", Wed, 14 Nov 2012 10:49:01 UTC +00:00], ["from_location_id", 1], | |
| ["name", "thuis-werk"], ["to_location_id", 2], ["updated_at", Wed, 14 Nov 2012 10:49:01 UTC +00:00]] | |
| # uiteindelijke data | |
| irb(main):007:0> Route.first | |
| => #<Route id: 1, name: "thuis-werk", from_location_id: 1, to_location_id: 2, created_at: "2012-11-14 10:49:01", updated_at: "2012-11-14 10:49:01"> | |
| irb(main):008:0> Route.first.to_location | |
| => #<Location id: 2, name: "Weert", created_at: "2012-11-14 10:47:54", updated_at: "2012-11-14 10:47:54"> | |
| irb(main):009:0> Route.first.from_location | |
| => #<Location id: 1, name: "Maastricht", created_at: "2012-11-14 10:47:46", updated_at: "2012-11-14 10:47:46"> |
Super!, dank je wel, dit was idd. precies wat ik zocht.
hmm,
route.from_location = Location.find_by_name('Maastricht')
werkte niet
route.from_location_id = Location.first lijkt te werken, maar slaat niet op, ik krijg de from_location_id niet van nil af, wat ik probeer geeft me steeds zoiets:
ActiveRecord::AssociationTypeMismatch: Mappoint(#70115712672580) expected, got Fixnum(#70115689435420)
geeft mij een probleem met
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Goede oplossing!