我无法找到此范围方法的正确措辞。任务是创建一个范围方法,该方法指定请求(具有注释)以仅在注释中显示带有单词“ kids”的请求。我似乎无法通过正确的措辞来使它起作用
我尝试过scope :requests_with_kids_in_note, -> { where('note').includes(text: "kids")}
scope :requests_with_kids_in_note, -> { select('note').where('note').includes(text: "kids")}
错误消息没有帮助
答案 0 :(得分:0)
您可以使用Arel
来组成该查询。
假设您有一个Request
关联的has_many :notes
,则可以这样编写范围:
class Request < ApplicationRecord
has_many :notes
scope :with_kids_in_note, -> {
joins(:notes).where(Note.arel_table[:content].matches('%kids%'))
}
end
class Note < ApplicationRecord
# In this model, i'm assuming that "content" is the column
# in which you have to find the word "kids".
belongs_to :request
end
答案 1 :(得分:0)
您可以通过作用域很容易地做到这一点,但是应该以不同的方式设置where子句的格式。另外,我建议使其更通用:
scope :note_contains, -> (word) { where('note LIKE %?%', word) }
并像这样使用它:
Model.note_contains('kids')