0

我有 2 个表,帐户和订阅。所有帐户都有订阅,我什么时候订阅

Account.last.subscription.name
  Account Load (0.4ms)  SELECT "accounts".* FROM "accounts" ORDER BY "accounts"."id" DESC LIMIT 1
  Subscription Load (0.2ms)  SELECT "subscriptions".* FROM "subscriptions" WHERE "subscriptions"."account_id" = 73 LIMIT 1
"free"

但我试图让所有设置为免费的帐户。

4

1 回答 1

3

您的问题中的细节很少,因此我们必须做出一些假设。假设Subscription有一个布尔free列。

# Rails 4
Account.joins(:subscriptions).where(free: true).references(:subscriptions)

# Rails 3
Account.joins(:subscriptions).where('subscriptions.free = ?', true)

如果我们假设free是一个 Account 属性:

Account.where(free: true)

如果您想提取订阅详细信息:

Account.where(free: true).includes(:subscriptions)
于 2013-07-11T20:42:09.110 回答