I am trying to create a has_many through relation with multiple sources.
For example a game has a home_team and away_team and a tournament has multiple games.
What is the best way to get all teams in the tournament using a has_many through games relation.
Right now my code looks like this:
class Tournament
has_many :teams, :through => :games, :source => :home_team, :uniq => true
end
but I want some way to make it act like:
class Tournament
has_many :teams, :through => :games, :source => [:home_team, :away_team], :uniq => true
end
EDIT: The many to many relationship is not my problem. Is there a good way to get all the teams in the tournament assuming the structure as follows.
class Game
has_and_belongs_to_many :tournaments
belongs_to :home_team, :class_name => Team, :foreign_key => :home_team_id
belongs_to :away_team, :class_name => Team, :foreign_key => :away_team_id
end
class Tournament
has_and_belongs_to_many :games
end
Is there a way to do Tournament.teams?
After spending some time trying to find a built in solution I just ended up writing a custom query called teams in games. It joins teams to games twice through team_1 and team_2 and checks to see if any of the list of game id's are in either of these two joins.
This solution isn't great since it requires multiple queries (One of which is just a huge list of all game ids), but I spent a lot of time trying to come up with another way and couldn't. At least this way works.
I'd love to learn a better way.
Code inside of games:
def self.teams
joined_tables = Team.joins(:home_team).joins(:away_team)
joined_tables.where('games.id in (?) or away_team_games.id in (?)',
select(:id), select(:id)).uniq
end
Define models like this:
class Tournament
has_many :games
has_many :teams, :through => :games
end
class Game
belongs_to :torunament
belongs_to :team
end
class Team
has_many :games
has_many :tournaments, :through => :games
end
And then call in controller, or wherever:
tournament.teams
EDIT:
you could define scope for this kind of problem in you Tournament model. This is more like some custom query, rather it is supported by rails out of the box. Or at least I can't remember at this very moment.
you can look how to use them.
http://guides.rubyonrails.org/active_record_querying.html
http://apidock.com/rails/ActiveRecord/NamedScope/ClassMethods/scope
http://ablogaboutcode.com/2011/02/19/scopes-in-rails-3/
You can build up a query that would get all the teams. You can create any query you like.
Related
I currently have essentially 3 models at the moment. I have Project, User and Contact.
I'm trying to assign users or contacts as a sort of 'member' to each project. I initially thought that a linking table here would suffice, for example ProjectMembers but i'm currently hitting a brick wall in my thought process when it comes to a project member only being allowed to be either a Contact or a User and whether to handle this via a relationship or by code in the model or the controller by checking which between user_id or contact_id was not null.
I had a look at polymorphic associations which looked promising, but somehow I ended up with the association backwards (Projects were being entered into the ProjectMember table as the type, rather than User or Contact) and confused myself even more.
The final output I would pretty much like would be simply to have the ability to run something like Project.first.project_members and have those members return with the role in that project. It'd be even nicer if I could run User.first.projects/Contact.first.projects and get those too, but that's something I can figure out down the line.
Any pointers would be greatly appreciated.
Polymorphic association is something that you should go for here.
First: Project has a many to many relationship with User and Contact, hence you need a join table.
Second: Since you need to call something like Project.first.project_members, you should have a polymorphic association.
For this scenario, you should create a polymorphic join table.
Let's say you have the polymorphic table ProjectMember with memberable_id, memberable_type and project_id (well I have a hang of railcasts).
class ProjectMember < ActiveRecord::Base
belongs_to :memberable, :polymorphic => true
belongs_to :project
end
and then in your User model,
class User < ActiveRecord::Base
has_many :project_members, :as => :memberable
has_many :projects, through: :project_members
end
and in Contact model
class Member < ActiveRecord::Base
has_many :project_members, :as => :memberable
has_many :projects, through: :project_members
end
and in your Project model
class Project < ActiveRecord::Base
has_many :users, :through => :project_members, :source => :memberable, :source_type => 'User'
has_many :contacts, :through => :project_members, :source => :memberable, :source_type => 'Contact'
has_many :project_members
end
Now you can call your desired associations, Project.first.project_members, User.first.projects or Contact.first.projects. Hope this helps.
Thanks
I have a model of users and things
Users can have one leader and many followers which are other users. User can also have things
So I have these definitions which use a self join relationship:
class User < ActiveRecord::Base
has_many :things, dependent: :destroy
has_many :followers, :class_name => 'User', :foreign_key => 'leader_id'
belongs_to :leader, :class_name => 'User', :foreign_key => 'leader_id'
end
class Thing < ActiveRecord::Base
belongs_to :user
end
So I can query ask for a list of things that a user has by asking, for example User.first.things. I can also get a list of followers of a user with User.first.followers.
How do I get a list of things that a user's followers have. I think I might need to use a has_many through relationship but I can't seem to figure it out as I'm not sure how to deal with the fact that a Leader can have things through a 'follower' but also directly themselves
Thanks
Something like this:
def all_things
Thing.where(:user_id => followers.map(&:id).push(id))
end
It returns a scope so you should be able to continue the chain, for example:
User.first.all_things.visible
Update
If you are only interested in the followers' things without adding the user's things to the batch it is better is you do it directly with a has_many through:
has_many :followers_things, :through => :followers, :source => :things
Check this other SO thread
How about:
all_things = (your_user.followers.each { |f| f.things }).flatten
This should return an array containing things that belong to all your_user's followers.
I'm trying to find the best way to model a game in relation to teams.
The end goal is to be able to call things like:
#game.winner
#game.loser
#team.games
The first two relations are working, but the games one is not. Using has_many (see below), I get ERROR: column games.team_id does not exist which I would normally work around by using whatever the equivalent to :foreign_key => winner_id, but how can I have it checkout both winner_id and loser_id?
Is the only option to create a method in the teams model like so:
def games
won = Game.where(:winner => id)
lost = Game.where(:loser => id)
won + lost
end
So far what I'm doing is:
class Game < ActiveRecord::Base
has_one :winner, class_name: "Team"
has_one :loser, class_name: "Team"
end
class Team
has_many :games
# or something that actually works
end
You didn't say much about the problem. But I think you are working too hard. If a team can play several games and a game includes more than one team, then you you need a many to many relation. This requires a third table, and best practice is a has_many :through relation. It will look something like:
class Game < ActiveRecord::Base
has_many :teams, through: assignment
has_one :winner, class_name: 'Team', through: :assignment, order: 'score DESC'
has_one :loser, class_name: 'Team', through: :assignment, order: 'score ASC'
end
class Team
has_many :games, through: :assignment
end
class Assignment < ActiveRecord::Base
belongs_to :game
belongs_to :team
end
Now you have the winner and loser attributes, but you don't need to roll your own method to count games for a team. Just say team.games.count and similarly game.teams is the teams assigned to the game.
class User < ActiveRecord::Base
has_many :books
has_many :book_users
has_many :books, :through => :book_users
end
class Book < ActiveRecord::Base
belongs_to :user
has_many :book_users
has_many :users, :through => :book_users
end
An user can write many books
An book can belong to only one user
An user can be a reader of different books
An book can be read by different users
User.books
should give me the books the user has written
User.books_read
should give me the books, are read by this user
How accomplish this ?
Second question, what is the simplest method to delete book_read from the user ?
I mean
User.method_name(book_id) # what's the method name ?
First question:
You either use :source or :class_name.
has_many :books_read, :class_name => "Book", :through => :book_users
I don't know for sure if :class_name works with has_many :through. If it doesn't work, try this:
has_many :books_read, :source => :book, :through => :book_users
That should do the trick.
Second question:
As far as I know there isn't really a simple method to delete books from the books_read relation. You could create your own method to accomplish this. Just make sure you delete records from :book_users and not the has_many :through relation. Or else the books themselves will be deleted.
def delete_book(book_id)
self.book_users.find_by_book_id(book_id).destroy
end
When using Rails 3 you can't use the find_by_... helper and need to use where.
def delete_book(book_id)
self.book_users.where(:book_id => book_id).destroy
end
Now you can call this function as follows:
User.find(1).delete_book(2)
I hope that helps you out.
What is the best way to achieve a has two association with activerecord?
I have a Team and Game models. Each Team will have_many games #team.games. A Game will have two teams #game.hosting_team and #game.opposing_team.
I started out with two belongs_to/has_one associations but then #team.games would only return their home games.
The other option I can think of is using a HABTM and use a validator to ensure there are only records. The only thing missing is keeping track of the home team. It seems like I need a has many through association but I'm not exactly sure...
Thanks for your help.
This is an example of how the two has_many associations look. The problem here is I would have to call team.games and team.opponents to get a full list of their games
class Team < ActiveRecord::Base
has_many :games
has_many :opponents, :class_name => "Team"#, :foreign_key => ""
end
class Game < ActiveRecord::Base
belongs_to :team, :class_name => "Team" #, :foreign_key => "team_id"
belongs_to :opponent, :class_name => "Team" #, :foreign_key => "opponent_id"
end
I'd like something like this but this obviously isn't how belongs_to works.
class Team < ActiveRecord::Base
has_many :games
end
class Game < ActiveRecord::Base
belongs_to :hosting_team
belongs_to :opposing_team
end
My desired api would look like this.
#team.games # return all games home or away
#game.hosting_team # Team
#game.opposing_team # Team
You can probably still model it with the bt/ho associations, and set up games as an accessor method on the team instead of as an association:
class Team < ActiveRecord::Base
def games
Game.find(:conditions => ["home_team_id = ? OR away_team_id = ?", id, id])
end
end