Skip to content

Instantly share code, notes, and snippets.

@emil-palm
Created April 15, 2012 12:58
Show Gist options
  • Save emil-palm/2392659 to your computer and use it in GitHub Desktop.
Save emil-palm/2392659 to your computer and use it in GitHub Desktop.
Join model assoc
Class match:
has_many :match_maps
has_many :maps, :through => :match_maps, :conditions => {'match_maps.veto' => false}, :source => :map, :class_name => "Map"
has_many :vetos, :through => :match_maps, :conditions => { 'match_maps.veto' => true}, :source => :map, :class_name => "Map"
m = Match.find(1)
m.vetos << Map.find(1)
m.map << Map.find(2)
@emil-palm
Copy link
Author

=> #<Map id: 2, name: "asdf", created_at: "2012-04-15 13:16:33", updated_at: "2012-04-15 13:16:33">
1.9.2-p290 :063 > m.vetos << map
(0.1ms) begin transaction
SQL (0.3ms) INSERT INTO "match_maps" ("map_id", "match_id", "veto") VALUES (?, ?, ?) ["map_id", 2], ["match_id", 1], ["veto", nil] commit transaction

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment