fuyi
fuyi

Reputation: 2639

merge ActiveRecord::Relation with ActiveRecord::Associations

I have 2 models

class User < ActiveRecord::Base
    has_many :games

    def created_games
        Game.where(created_by: self.id)
    end
end

class Game < ActiveRecord::Base
    belongs_to :user
end

u = User.take
joined_games = u.games
created_games = u.created_games

the variable joined_games is a instance of ActiveRecord::Associations, and created_games is an instance of ActiveRecord::Relation.

Is there any way I can join joined_games and created_games together?

Upvotes: 2

Views: 129

Answers (1)

Himesh
Himesh

Reputation: 646

Try adding a scope in User model

scope :joined_games, where(user_id: self.id, created_by: self.id)

Upvotes: 1

Related Questions