gpt4 book ai didi

ruby-on-rails - Rails Activerecord 多个表包括

转载 作者:行者123 更新时间:2023-12-03 16:02:36 24 4
gpt4 key购买 nike

我有四个表:

参数带字段

  • id

  • 评论
  • id
  • comment_id
  • 参数_id
  • 用户 ID

  • 用户
  • id

  • 昵称
  • id
  • proposal_id
  • 用户 ID
  • 姓名

  • 每个论点都有很多评论,

    每条评论都属于一个用户,

    每个用户在参数中都有一个特定的昵称。

    当我从 DB 获取参数评论时,我还想包括每个作者的昵称。

    答案是关于 ActiveRecord查询不知道怎么写 .

    我试过
    @argument.comments.includes(:user => :nicknames)

    但它似乎不起作用,当我通过昵称时
    昵称 = @argument.nicknames.find_by_user_id(comment.user.id)
    它执行查询...
    [1m[36mNickname Load (0.6ms)[0m  [1mSELECT "nicknames".* FROM "nicknames" WHERE "nicknames"."argument_id" = 59 AND "nicknames"."user_id" = 9 LIMIT 1[0m

    有什么建议吗?

    最佳答案

    您可以判断关联是否加载了 loaded? .

    如果我理解您的问题,这里发生的事情是您正在尝试在 ActiveRecord::Relation 上运行查找程序。快速浏览the code ,它似乎不会在发出查询之前尝试查看是否加载了集合。但是,它确实需要一个可以避免多次查询的块。例如(模型名称已更改,因为我正在使用我为另一个问题创建的示例项目):

    c = Canteen.first
    Canteen Load (0.2ms) SELECT "canteens".* FROM "canteens" LIMIT 1
    => #<Canteen id: 1, name: "Really good place", created_at: "2012-12-13 00:04:11", updated_at: "2012-12-13 00:04:11">

    c.meals.loaded?
    => false

    c.meals.find {|m| m.id == 3}
    Meal Load (0.2ms) SELECT "meals".* FROM "meals" WHERE "meals"."canteen_id" = 1
    => #<Meal id: 3, canteen_id: 1, name: "Banana Pie", price: #<BigDecimal:7fcb6784fa78,'0.499E1',18(45)>, created_at: "2012-12-13 00:37:41", updated_at: "2012-12-13 00:37:41">

    您在上一个示例中看到 ActiveRecord 发出查询以加载关联的记录。这是因为 ActiveRecord 正在调用 to_a在关联上,强制加载整个集合,然后根据块条件进行过滤。显然,这并不理想。

    让我们再试一次,急切地加载关联。
    c = Canteen.includes(:meals).first
    Canteen Load (0.2ms) SELECT "canteens".* FROM "canteens" LIMIT 1
    Meal Load (0.2ms) SELECT "meals".* FROM "meals" WHERE "meals"."canteen_id" IN (1)
    => #<Canteen id: 1, name: "Really good place", created_at: "2012-12-13 00:04:11", updated_at: "2012-12-13 00:04:11">

    c.meals.loaded?
    => true

    c.meals.find {|m| m.id == 3}
    => #<Meal id: 3, canteen_id: 1, name: "Banana Pie", price: #<BigDecimal:7fcb68b596f0,'0.499E1',18(45)>, created_at: "2012-12-13 00:37:41", updated_at: "2012-12-13 00:37:41">

    在此处的最后一个示例中,您会看到该集合未再次加载。相反,该块用于过滤已加载的记录。

    正如你在下面看到的,即使记录被加载,ActiveRecord 也会发出查询来获取关联的记录:
    c.meals.loaded?
    => true

    c.meals.find(1)
    Meal Load (0.1ms) SELECT "meals".* FROM "meals" WHERE "meals"."canteen_id" = 1 AND "meals"."id" = ? LIMIT 1 [["id", 1]]
    => #<Meal id: 1, canteen_id: 1, name: "Enchiladas", price: #<BigDecimal:7fcb6584ce88,'0.699E1',18(45)>, created_at: "2012-12-13 00:04:40", updated_at: "2012-12-13 00:04:40">

    SELECT "meals".* FROM "meals" WHERE "meals"."canteen_id" = 1 AND "meals"."id" = 3
    => [#<Meal id: 3, canteen_id: 1, name: "Banana Pie", price: #<BigDecimal:7fcb68b808e0,'0.499E1',18(45)>, created_at: "2012-12-13 00:37:41", updated_at: "2012-12-13 00:37:41">]

    关于ruby-on-rails - Rails Activerecord 多个表包括,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12167606/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com