class Program
attr_accessible :open, ...
has_many :rounds
has_many :open_rounds, :class_name => 'Round', :conditions => ['open = ?', true]
end
class Round
belongs_to :program
attr_accessible :open, :status, :begin ...
end
[64] pry(main)> a = Program.includes(:rounds)
Program Load (0.2ms) SELECT "programs".* FROM "programs"
Round Load (0.2ms) SELECT "rounds".* FROM "rounds" WHERE "rounds"."program_id" IN (4, 6)
+----+-------------+-------------+------------+-------+--------+-------------+------------+-------------+
| id | name | moderato... | descrip... | open | locked | suppress... | created_at | updated_at |
+----+-------------+-------------+------------+-------+--------+-------------+------------+-------------+
| 4 | Advanced... | | Summer ... | false | true | false | 2013-04... | 2013-04-... |
| 6 | Introduc... | | Summer ... | false | true | false | 2013-04... | 2013-04-... |
+----+-------------+-------------+------------+-------+--------+-------------+------------+-------------+
2 rows in set
[65] pry(main)>
SQL看起来像我期望的那样,但我希望在查询结果中看到Round的其他列。
示例二是相同但更令人困惑的。请注意,计划4没有公开轮次:
[67] pry(main)> Program.find(6).rounds
Program Load (0.1ms) SELECT "programs".* FROM "programs" WHERE "programs"."id" = ? LIMIT 1 [["id", 6]]
Round Load (0.2ms) SELECT "rounds".* FROM "rounds" WHERE "rounds"."program_id" = 6
+----+------------+--------+-------+-----+--------+-------+-----------+----------------+----------------+
| id | program_id | number | start | fin | status | open | open_date | created_at | updated_at |
+----+------------+--------+-------+-----+--------+-------+-----------+----------------+----------------+
| 10 | 6 | 0 | 0 | 10 | | false | | 2013-04-13 ... | 2013-04-13 ... |
| 11 | 6 | 1 | 11 | 21 | | false | | 2013-04-13 ... | 2013-04-13 ... |
| 12 | 6 | 2 | 22 | 32 | | false | | 2013-04-13 ... | 2013-04-13 ... |
| 13 | 6 | 3 | 33 | 43 | | false | | 2013-04-13 ... | 2013-04-13 ... |
+----+------------+--------+-------+-----+--------+-------+-----------+----------------+----------------+
4 rows in set
然而,当我这样做时:
[68] pry(main)> a = Program.includes(:open_rounds)
Program Load (0.2ms) SELECT "programs".* FROM "programs"
Round Load (0.2ms) SELECT "rounds".* FROM "rounds" WHERE "rounds"."program_id" IN (4, 6) AND (open = 'f')
+----+-------------+-------------+------------+-------+--------+-------------+------------+-------------+
| id | name | moderato... | descrip... | open | locked | suppress... | created_at | updated_at |
+----+-------------+-------------+------------+-------+--------+-------------+------------+-------------+
| 4 | Advanced... | | Summer ... | false | true | false | 2013-04... | 2013-04-... |
| 6 | Introduc... | | Summer ... | false | true | false | 2013-04... | 2013-04-... |
+----+-------------+-------------+------------+-------+--------+-------------+------------+-------------+
我不仅没有看到Round属性,而且还看到了我没想到的程序6。除了" AND(open =' f')之外,SQL看起来还不错。因为注意到程序和回合都有一个名为open的列。
很抱歉所有的转储,但我想清楚地列出证据。想法?