- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
有一个带有枚举的 Post 模型
// Post.rb
enum category: {
job: 'job',
conference: 'conference'
}
一个组织拥有并属于许多帖子(通过连接表)
Org.includes(:posts).where(category: Post.categories[:job])
它似乎在尝试调用 Org 上的类别。有没有办法只写这个返回包含帖子的 Org,其中帖子有字符串“job”的枚举?
最佳答案
tl;dr 使用
Org.includes(:posts).where(posts: {category: :job})
较长的答案...
我想值得注意的是,您的问题实际上与enums
没有任何关系。它也与“包括另一个模型”无关。你真正想做的是 Specify Conditions on the Joined Tables您可以在 Active Record Query Interface guide 中阅读更多相关信息.
问题是您的 ActiveRecord 查询格式不正确:
Org.includes(:posts).where(category: Post.categories[:job])
您当前拥有的基本形式是:
Model.where(attribute: 'value')
:
.includes(:joined_models)
...位不会改 rebase 本形式。因此,ActiveRecord
将返回所有 Model
记录,其中 attribute
具有 value
。或者,在您的情况下,所有 Org
模型,其中 category
是 job
。
但是,这不是您想要的。您需要所有具有 Posts
的 Orgs
,其中 Post
category
是 job
。 (或者,我想,“所有有职位的组织。”)
这就是 .includes(:joined_models)
位的用武之地:它允许您在 joined_models
上指定条件,其基本形式如下所示:
Model.includes(:joined_models).where(joined_models: {attribute: 'value'})
^^^^^^^^^^^^^
或者,在您的情况下:
Org.includes(:posts).where(posts: {category: Post.categories[:job]})
或者,正如 mu 在评论中所说:
Org.includes(:posts).where(posts: {category: :job})
现在,我不知道你在什么上下文中,但无论你在哪里,上面的代码都要求你的上下文了解很多关于 Org
以及它与 Post 的关系
和 Post
的属性通常不是很好。所以,我建议你向 Org
添加一个方法,它允许你在你的上下文中解耦 Org
的知识:
class Org < ApplicationRecord
class << self
def with_job_posts
includes(:posts).where(posts: {category: :job}})
end
end
end
现在你可以简单地做:
Org.with_job_posts
...并返回“所有有职位的组织”。而且您的上下文需要了解的关于 Post
及其属性的信息要少得多。
Post
还有一个类别 conference
。所以,你可以这样做:
class Org < ApplicationRecord
class << self
def with_job_posts
includes(:posts).where(posts: {category: :job}})
end
def with_conference_posts
includes(:posts).where(posts: {category: :conference}})
end
end
end
但是,如果您的 Post
categories
开始增长,那将变得乏味。所以,改为:
class Org < ApplicationRecord
Post.categories.each do |post_category|
define_singleton_method("#{post_category}"_posts) do
includes(:posts).where(posts: {category: post_category.to_sym})
end
end
end
现在您将拥有任意数量的方法,例如:
Org.with_job_posts
Org.with_conference_posts
Org.with_some_other_type_of_posts
super 棒!查看this Q&A更多信息来自 Jörg W Mittag .
顺便说一句,这看起来像是一种使用 enum
的潜在不寻常的方式。在docs ,它说:
Finally, it's also possible to explicitly map the relation between attribute and database integer with a hash:
class Conversation < ActiveRecord::Base
enum status: { active: 0, archived: 1 }
end
我一直认为映射枚举意味着使用整数作为值,而不是字符串。有趣。
关于ruby-on-rails - 事件记录 : How to include another model based on Enum?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58616540/
我对 mongoosejs 中模型的使用感到有些困惑。 可以通过这些方式使用 mongoose 创建模型 使用 Mongoose var mongoose = require('mongoose');
我正在看 from django.db import models class Publisher(models.Model): name = models.CharField(max_len
我有自己的 html 帮助器扩展,我用这种方式 model.Reason_ID, Register.PurchaseReason) %> 这样声明的。 public static MvcHtmlS
假设模型原本是存储在CPU上的,然后我想把它移到GPU0上,那么我可以这样做: device = torch.device('cuda:0') model = model.to(device) # o
我过去读过一些关于模型的 MVC 建议,指出不应为域和 View 重用相同的模型对象;但我找不到任何人愿意讨论为什么这很糟糕。 我认为创建两个单独的模型 - 一个用于域,一个用于 View - 然后在
我正在使用pytorch构建一个像VGG16这样的简单模型,并且我已经重载了函数forward在我的模型中。 我发现每个人都倾向于使用 model(input)得到输出而不是 model.forwar
tf.keras API 中的 models 是否多余?对于某些情况,即使不使用 models,代码也能正常运行。 keras.models.sequential 和 keras.sequential
当我尝试使用 docker 镜像运行 docker 容器时遇到问题:tensorflow/serving。 我运行命令: docker run --name=tf_serving -it tensor
我有一个模型,我用管道注册了它: register_step = PythonScriptStep(name = "Register Model",
如果 View 需要访问模型中的数据,您是否认为 Controller 应: a)将模型传递给 View b)将模型的数据传递给 View c)都不;这不应该是 Controller 所关心的。让 V
我正在寻找一个可以在模型中定义的字段,该字段本质上是一个列表,因为它将用于存储多个字符串值。显然CharField不能使用。 最佳答案 您正在描述一种多对一的关系。这应该通过一个额外的 Model 进
我最近了解了 Django 中的模型继承。我使用很棒的包 django-model-utils 取得了巨大的成功。我继承自 TimeStampedModel 和 SoftDeletableModel。
我正在使用基于 resnet50 的双输出模型进行项目。一个输出用于回归任务,第二个输出用于分类任务。 我的主要问题是关于模型评估。在训练期间,我在验证集的两个输出上都取得了不错的结果: - 综合损失
我是keras的新手。现在,我将使用我使用 model.fit_generator 训练的模型来预测测试图像组。我可以使用 model.predict 吗?不确定如何使用model.predict_g
在 MVC 应用程序中,我加入了多个表并将其从 Controller 返回到 View,如下所示: | EmployeeID | ControlID | DoorAddress | DoorID |
我在使用 sails-cassandra 连接系统的 Sails 中有一个 Data 模型。数据。 Data.count({...}).exec() 返回 1,但 Data.find({...}).e
我正在使用 PrimeFaces dataTable 开发一个 jsf 页面来显示用户列表。用户存储在 Model.User 类的对象中。
我正在关注https://www.tensorflow.org/tutorials/keras/basic_classification解决 Kaggle 挑战。 但是,我不明白应该将什么样的数据输入
我是这个领域的新手。那么,你们能帮忙如何为 CNN 创建 .config 文件吗? 传递有关如何执行此操作的文档或教程将对我有很大帮助。谢谢大家。 最佳答案 这个问题对我来说没有多大意义,因为 .co
我是“物理系统建模”主题的新手。我阅读了一些基础文献,并在 Modelica 和 Simulink/Simscape 中做了一些教程。我想问你,如果我对以下内容理解正确: 符号操作是将微分代数方程组(
我是一名优秀的程序员,十分优秀!