【问题标题】:Is there a way to make a model method in active record?有没有办法在活动记录中制作模型方法?
【发布时间】:2019-08-19 16:34:03
【问题描述】:

我正在考虑创建一个方法,该方法将给我来自 Active::Record::Associations::CollectionProxy 的特定对象。

例如,我有一个用户模型,它有许多 user_checklists 和 user_checklists belongs_to checklist 模型。所以我想创建一个方法来检查清单模型中的值并从 user_checklist 模型中返回值。

例如。 checklists 表有以下一行

╔═════════════════════╦══════════════════╗
║         id          ║    name          ║
╠═════════════════════╬══════════════════╣
║          1          ║ Complete MBA     ║
╠═════════════════════╬══════════════════╣
║          2          ║ Painting class   ║
╚═════════════════════╩══════════════════╝

用户表有以下内容

╔═════════════════════╦══════════════════╗
║         id          ║    name          ║
╠═════════════════════╬══════════════════╣
║          1          ║    name          ║
╚═════════════════════╩══════════════════╝

user_checklist 有以下一行

╔════════════╦════════════╦═══════════════╦═══════════════╗
║    id      ║    user_id ║ checklist_id  ║   completed   ║
╠════════════╬════════════╬═══════════════╬═══════════════╣
║     1      ║      1     ║      1        ║     true      ║
╠════════════╬════════════╬═══════════════╬═══════════════╣
║     2      ║      1     ║      2        ║     false     ║
╚════════════╩════════════╩═══════════════╩═══════════════╝

所以我想在 user_checklist 中创建一个名为 MBA 的方法,它可以为我提供 MBA 完成值。

我想这样称呼 user.first.user_checklists.mba.completed

我不知道如何实现这一点。还是我在这里完全想错了?

【问题讨论】:

  • 您是否需要一种方法来返回用户是否完成了清单(任何清单)或仅特定值(MBA)@gsumk?
  • 具体值。在这种情况下,我浪费了 completed MBA @Aarthi 的价值

标签: ruby-on-rails activerecord rails-activerecord


【解决方案1】:

我建议只选择清单的 id。

scope :mba, -> { where(checklist_id: Checklist.where(name: "Completed MBA").pluck(:id) }

将仅从数据库中查询所需的列(id)(具有更好的性能),

 SELECT "checklists"."id" FROM "checklists" WHERE "checklists"."id" = $1

你可以得到和你所做的一样的

User.first.user_checklists.mba.first&.completed

【讨论】:

    【解决方案2】:

    您可以更新您的范围以避免 N+1 查询:

    scope :mba, -> { joins(:checklists).where('checklists.name = "Completed MBA"')
    

    您不需要在您的范围内选择第一个和 id,因为我猜您在下一个请求中调用 first 方法:

    User.first.user_checklists.mba.first&.completed

    【讨论】:

      【解决方案3】:

      所以我想出了一个办法。在 user_checklists 上创建一个范围以查找 mba_id 并仅从 user_checkilust 中返回该值

      scope :mba, -> { where(checklist_id: Checklist.where(name: "Completed MBA").first&.id) }
      
      

      之后我就可以了

      User.first.user_checklists.mba.first&.completed
      

      所以现在对我有用。

      【讨论】:

        猜你喜欢
        • 2020-04-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-19
        • 2013-09-20
        • 1970-01-01
        • 2021-12-24
        • 1970-01-01
        相关资源
        最近更新 更多