Skip to content

Instantly share code, notes, and snippets.

@kenmazaika
Created May 7, 2015 00:44
Show Gist options
  • Save kenmazaika/49d36bb5fe9868d31e4b to your computer and use it in GitHub Desktop.
Save kenmazaika/49d36bb5fe9868d31e4b to your computer and use it in GitHub Desktop.
Places
------
id
Comments
------
id
place_id
class: Place
database table: places
has_many :comments -> place_id -> Comment
has_many :white_games
has_many :black_games
def all_games
white_games + black_games
end
id | name
1 | Pizza
2 | Nom
3 | Nom
4 | nom
class: Comment
database table: comments
belongs_to :place -> :place_id -> places.id -> Place
belongs_to :awesome_place, :foreign_key => :awesome_place_id, :class_name => 'Place'
belongs_to :terrible_place, :foreign_key => :terrible_place_id, :class_name => 'Place'
id | awesome_place_id | terrible_place_id
10 | 2 | 5
11 | 2 | 3
12 | 3 | 4
c = Comment.last
c.awesome_place => look up by awesome_place_id
add_index :comments, [:awesome_place_id, :terrible_place_id]
add_index :comments, :terrible_place_id
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment