Created
September 27, 2012 20:51
-
-
Save lessless/3796394 to your computer and use it in GitHub Desktop.
has_many :through
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 Meeting < ActiveRecord::Base | |
has_many :participations, dependent: :destroy | |
has_many :players, through: :participations | |
belongs_to :place | |
attr_accessible :place_id, :name | |
end | |
class Participation < ActiveRecord::Base | |
belongs_to :meeting | |
belongs_to :player | |
attr_accessible :meeting_id, :player_id | |
end | |
class Place < ActiveRecord::Base | |
has_many :meetings | |
attr_accessible :address | |
end | |
class Player < ActiveRecord::Base | |
has_many :participations | |
has_many :meetings, through: :participations | |
attr_accessible :name | |
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
1.9.3-p125 :005 > john=Player.create(name:'John') | |
(0.1ms) begin transaction | |
SQL (112.6ms) INSERT INTO "players" ("created_at", "name", "updated_at") VALUES (?, ?, ?) [["created_at", Thu, 27 Sep 2012 20:48:12 UTC +00:00], ["name", "John"], ["updated_at", Thu, 27 Sep 2012 20:48:12 UTC +00:00]] | |
(154.4ms) commit transaction | |
=> #<Player id: 1, name: "John", created_at: "2012-09-27 20:48:12", updated_at: "2012-09-27 20:48:12"> | |
1.9.3-p125 :006 > Place.create(address: 'central park') | |
(0.1ms) begin transaction | |
SQL (0.6ms) INSERT INTO "places" ("address", "created_at", "updated_at") VALUES (?, ?, ?) [["address", "central park"], ["created_at", Thu, 27 Sep 2012 20:48:33 UTC +00:00], ["updated_at", Thu, 27 Sep 2012 20:48:33 UTC +00:00]] | |
(128.2ms) commit transaction | |
=> #<Place id: 1, address: "central park", created_at: "2012-09-27 20:48:33", updated_at: "2012-09-27 20:48:33"> | |
1.9.3-p125 :007 > john.meetings.create(name:'PvP,5x2,3/4', place_id: Place.first.id).participations.create | |
Place Load (0.2ms) SELECT "places".* FROM "places" LIMIT 1 | |
(0.1ms) begin transaction | |
SQL (0.5ms) INSERT INTO "meetings" ("created_at", "name", "place_id", "updated_at") VALUES (?, ?, ?, ?) [["created_at", Thu, 27 Sep 2012 20:48:42 UTC +00:00], ["name", "PvP,5x2,3/4"], ["place_id", 1], ["updated_at", Thu, 27 Sep 2012 20:48:42 UTC +00:00]] | |
SQL (0.5ms) INSERT INTO "participations" ("created_at", "meeting_id", "player_id", "updated_at") VALUES (?, ?, ?, ?) [["created_at", Thu, 27 Sep 2012 20:48:42 UTC +00:00], ["meeting_id", 1], ["player_id", 1], ["updated_at", Thu, 27 Sep 2012 20:48:42 UTC +00:00]] | |
(112.8ms) commit transaction | |
(0.1ms) begin transaction | |
SQL (0.5ms) INSERT INTO "participations" ("created_at", "meeting_id", "player_id", "updated_at") VALUES (?, ?, ?, ?) [["created_at", Thu, 27 Sep 2012 20:48:42 UTC +00:00], ["meeting_id", 1], ["player_id", nil], ["updated_at", Thu, 27 Sep 2012 20:48:42 UTC +00:00]] | |
(108.5ms) commit transaction | |
=> #<Participation id: 2, meeting_id: 1, player_id: nil, created_at: "2012-09-27 20:48:42", updated_at: "2012-09-27 20:48:42"> | |
1.9.3-p125 :008 > Participation.all | |
Participation Load (0.3ms) SELECT "participations".* FROM "participations" | |
=> [#<Participation id: 1, meeting_id: 1, player_id: 1, created_at: "2012-09-27 20:48:42", updated_at: "2012-09-27 20:48:42">, #<Participation id: 2, meeting_id: 1, player_id: nil, created_at: "2012-09-27 20:48:42", updated_at: "2012-09-27 20:48:42">] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment