I am creating an application to track football teams through out the season. but i am stuck on the design of the database. One fixture has a home team and an away team. I have created a fixture model which has two foreign keys- home_team and away_team but I cant get the association to work right. any ideas? Each fixture belongs to a league.
相关问题
- Question marks after images and js/css files in ra
- Using :remote => true with hover event
- Eager-loading association count with Arel (Rails 3
- Is there a way to remove IDV Tags from an AIFF fil
- Rails how to handle error and exceptions in model
相关文章
- Right way to deploy Rails + Puma + Postgres app to
- AWS S3 in rails - how to set the s3_signature_vers
- how to call a active record named scope with a str
- How to add a JSON column in MySQL with Rails 5 Mig
- “No explicit conversion of Symbol into String” for
- form_for wrong number of arguments in rails 4
- Rspec controller error expecting <“index”> but
- Factory_girl has_one relation with validates_prese
Say that you have a
Team
model and aSomething
Model, the latter will havehome_team_id
andaway_team_id
.You will refer to them as
something.away_team
andsomething.home_team
.The simple answer is:
But this is no good if you because Team.fixtures will not work.
will give you two collections… but aggregating them will have to happen in ruby, which will be icky.
This has it's problems too, sort and limit will be all ballsed up (heh, a pun, who knew?).
This is ugly, but may just work. The finder_sql seems to do what's needed.
The other option is to use a named_scope:
This last solution is the one I'd stick with, because it's a scope it will behave much like a read only association, and prevents the
:finder_sql
wackiness that may happen further down the line (I mean really? How does it know how to merge more conditions? It sometimes does a subquery, a subquery? Thats just not needed here? ).Hope this helps.