1
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 属性,而且看到了我没想到的 Program 6。除了“AND (open ='f') 之外,SQL 看起来还可以。因为请注意,在 Program 和 Round 中都有一个名为 open 的列。

对不起所有的转储,但我想清楚地列出证据。想法?

4

0 回答 0