0

我与以下活动记录模型(示例)有一对一的关联:

User <-1-1-> employee <-1-1-> contact <-1-1-> 
permanentAddress contact <-1-1-> communicationAddress    

当使用 ActiveRecord 触发查询时:

User Load (1.1ms)  SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", "49"]]
  Employeedetail Load (1.8ms)  SELECT "employeedetails".* FROM "employeedetails" WHERE "employeedetails"."User_id" = 49 LIMIT 1
  Contactdetail Load (0.6ms)  SELECT "contactdetails".* FROM "contactdetails" WHERE "contactdetails"."employeedetail_id" = 40 LIMIT 1
  Address Load (1.1ms)  SELECT "addresses".* FROM "addresses" WHERE "addresses"."contactdetail_id" = 52 LIMIT 1
  CACHE (0.0ms)  SELECT "addresses".* FROM "addresses" WHERE "addresses"."contactdetail_id" = 52 LIMIT 1

问题:如何将LIMIT地址表增加到 2 [inner association]

4

2 回答 2

1

我认为没有办法覆盖这个 has_one 和 belongs_to 案例中的限制,因为默认情况下,当我们编写 user.employer 时会触发的 SQL 设置了 LIMIT 1 约束......如果你想要一个联系人有两个地址那么您应该将关系从 has_one 更改为 has_many,然后使用验证对其应用 2 的限制...

于 2012-11-05T07:58:44.703 回答
0

您可以使用limit

Address.limit(2).where("contactdetail_id = ?", 52)

因此,它将返回前 2 个地址有 contactdetail_id = 52。查看更多Limit 和 Offset

于 2012-11-05T07:12:14.853 回答