Rails 控制台设置限制为一个查询
Rails console setting limit to one on queries
当我 运行 ActiveRecord 查询时,Rails 控制台似乎将 LIMIT 1
附加到我的查询中。
所以我有一个 sheet
,其中 has_many
slots
。当我查询 Slot.find_by(sheet_id: 96)
时,我得到:
Slot Load (2.3ms) SELECT "slots".* FROM "slots" WHERE "slots"."sheet_id" = ? LIMIT 1 [["sheet_id", 96]]
=> #<Slot id: 153, label: "Foo", name: "Foo", email: "", phone: "", comments: "Fighters", sheet_id: 96, created_at: "2015-04-30 14:28:47", updated_at: "2015-04-30 14:28:47">
但是当我查询时Sheet.find(96).slots
:
Sheet Load (10.0ms) SELECT "sheets".* FROM "sheets" WHERE "sheets"."id" = ? LIMIT 1 [["id", 96]]
Slot Load (4.6ms) SELECT "slots".* FROM "slots" WHERE "slots"."sheet_id" = ? [["sheet_id", 96]]
=> #<ActiveRecord::Associations::CollectionProxy [#<Slot id: 153, label: "Foo", name: "Foo", email: "", phone: "", comments: "Fighters", sheet_id: 96, created_at: "2015-04-30 14:28:47", updated_at: "2015-04-30 14:28:47">, #<Slot id: 154, label: "Bar", name: "James", email: "", phone: "", comments: "Foobar", sheet_id: 96, created_at: "2015-04-30 14:28:47", updated_at: "2015-04-30 14:28:47">, ... >
你必须做Slot.find_all_by_sheet_id(96)
EDIT 上面的代码应该有效。虽然我用的是Rails4.1.8。也可以尝试以下操作:
Slot.where(:sheet_id => 338)
find_by
方法 returns 一个结果,总是。
如果您想获得特定 sheet 的所有插槽,有几个选项:
Sheet.find(96).slots
或更有可能 @sheet.slots
如果您已经找到 sheet
Slot.where(sheet_id: 96)
也可以
明确地说,这与 Rails 控制台无关,与 .find_by
方法有关。
当我 运行 ActiveRecord 查询时,Rails 控制台似乎将 LIMIT 1
附加到我的查询中。
所以我有一个 sheet
,其中 has_many
slots
。当我查询 Slot.find_by(sheet_id: 96)
时,我得到:
Slot Load (2.3ms) SELECT "slots".* FROM "slots" WHERE "slots"."sheet_id" = ? LIMIT 1 [["sheet_id", 96]]
=> #<Slot id: 153, label: "Foo", name: "Foo", email: "", phone: "", comments: "Fighters", sheet_id: 96, created_at: "2015-04-30 14:28:47", updated_at: "2015-04-30 14:28:47">
但是当我查询时Sheet.find(96).slots
:
Sheet Load (10.0ms) SELECT "sheets".* FROM "sheets" WHERE "sheets"."id" = ? LIMIT 1 [["id", 96]]
Slot Load (4.6ms) SELECT "slots".* FROM "slots" WHERE "slots"."sheet_id" = ? [["sheet_id", 96]]
=> #<ActiveRecord::Associations::CollectionProxy [#<Slot id: 153, label: "Foo", name: "Foo", email: "", phone: "", comments: "Fighters", sheet_id: 96, created_at: "2015-04-30 14:28:47", updated_at: "2015-04-30 14:28:47">, #<Slot id: 154, label: "Bar", name: "James", email: "", phone: "", comments: "Foobar", sheet_id: 96, created_at: "2015-04-30 14:28:47", updated_at: "2015-04-30 14:28:47">, ... >
你必须做Slot.find_all_by_sheet_id(96)
EDIT 上面的代码应该有效。虽然我用的是Rails4.1.8。也可以尝试以下操作:
Slot.where(:sheet_id => 338)
find_by
方法 returns 一个结果,总是。
如果您想获得特定 sheet 的所有插槽,有几个选项:
Sheet.find(96).slots
或更有可能 @sheet.slots
如果您已经找到 sheet
Slot.where(sheet_id: 96)
也可以
明确地说,这与 Rails 控制台无关,与 .find_by
方法有关。