Skip to content

Instantly share code, notes, and snippets.

@kenmazaika
Created July 27, 2016 23:25
Show Gist options
  • Save kenmazaika/ee50cdf21f5c3604f5c36137c54f4779 to your computer and use it in GitHub Desktop.
Save kenmazaika/ee50cdf21f5c3604f5c36137c54f4779 to your computer and use it in GitHub Desktop.
class Game < ActiveRecord::Base
belongs_to :white_user, class_name: 'User', foreign_key: "white_user_id"
end
class User < ActiveRecord::Base
has_many :white_games, class_name: 'Game', foreign_key: "white_user_id" # => app/models/game.rb
has_many :black_games, class_name: 'Game', foreign_key: "black_user_id" # => app/models/game.rb
end
def create
@game = current_user.white_games.create(game_params)
end
def index
Game.where("black_user_id IS ? OR white_user_id IS ?",
current_user.id, current_user.id)
end
class Place < ActiveRecord::Base
has_many :comments, foreign_key: "place_id" # => app/models/comment.rb => Comment
end
# schema
create_table "comments" do | t|
t.integer :place_id
end
create_table "games" do |t|
t.integer :user_id
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment