Ruby on rails 过滤科学化和全球化
filterrific似乎没有考虑翻译表中的内容() 是否也可以搜索翻译表?如果内容在实际模型中,我的设置工作得非常好。但是,一旦字段为空且仅输入到翻译表中,则不会显示任何结果(显然) 我的模型:Ruby on rails 过滤科学化和全球化,ruby-on-rails,globalize,filterrific,Ruby On Rails,Globalize,Filterrific,filterrific似乎没有考虑翻译表中的内容() 是否也可以搜索翻译表?如果内容在实际模型中,我的设置工作得非常好。但是,一旦字段为空且仅输入到翻译表中,则不会显示任何结果(显然) 我的模型: class Manual < ApplicationRecord translates :title, :content, :teaser, :slug extend FriendlyId friendly_id :title, :use => :globalize b
class Manual < ApplicationRecord
translates :title, :content, :teaser, :slug
extend FriendlyId
friendly_id :title, :use => :globalize
belongs_to :user
belongs_to :support_category
has_many :manual_faqs
has_many :faqs, :through => :manual_faqs
validates :title, presence: true
validates :content, presence: true
validates :user_id, presence: true
update_index('manuals#manual') { self }
filterrific(
default_filter_params: { sorted_by: 'created_at_desc' },
available_filters: [
:sorted_by,
:search_query,
:with_user_id,
:with_created_at_gte
]
)
scope :with_user_id, lambda { |user_ids|
where(user_id: [*user_ids])
}
scope :search_query, lambda { |query|
# Searches the students table on the 'first_name' and 'last_name' columns.
# Matches using LIKE, automatically appends '%' to each term.
# LIKE is case INsensitive with MySQL, however it is case
# sensitive with PostGreSQL. To make it work in both worlds,
# we downcase everything.
return nil if query.blank?
# condition query, parse into individual keywords
terms = query.downcase.split(/\s+/)
# replace "*" with "%" for wildcard searches,
# append '%', remove duplicate '%'s
terms = terms.map { |e|
('%' + e.gsub('*', '%') + '%').gsub(/%+/, '%')
}
# configure number of OR conditions for provision
# of interpolation arguments. Adjust this if you
# change the number of OR conditions.
num_or_conds = 2
where(
terms.map { |term|
"(LOWER(manuals.title) LIKE ? OR LOWER(manuals.content) LIKE ?)"
}.join(' AND '),
*terms.map { |e| [e] * num_or_conds }.flatten
)
}
scope :sorted_by, lambda { |sort_option|
# extract the sort direction from the param value.
direction = (sort_option =~ /desc$/) ? 'desc' : 'asc'
case sort_option.to_s
when /^created_at_/
# Simple sort on the created_at column.
# Make sure to include the table name to avoid ambiguous column names.
# Joining on other tables is quite common in Filterrific, and almost
# every ActiveRecord table has a 'created_at' column.
order("manuals.created_at #{ direction }")
else
raise(ArgumentError, "Invalid sort option: #{ sort_option.inspect }")
end
}
scope :created_at_gte, lambda { |reference_time|
where('manuals.created_at >= ?', reference_time)
}
def self.options_for_sorted_by
[
['Date received (newest first)', 'created_at_desc'],
['Date received (oldest first)', 'created_at_asc']
]
end
end
我一点也不知道filterrific,但我知道Globalize,因为filterrific是基于AR范围的,所以只需加入转换表就可以得到显示的结果 以下是您的
search\u查询
范围,已修改为加入并搜索已加入的翻译表(为了清晰起见,不带注释):
请注意,我只更改了两件事:(1)我添加了一个方法,用于连接当前区域设置的翻译;(2)我将manuals
表替换为查询中的manual\u translations
表
因此,如果您在英语区域设置中调用此查询:
Manual.search_query("foo")
您将获得以下SQL:
SELECT "manuals".* FROM "manuals"
INNER JOIN "manual_translations" ON "manual_translations"."manual_id" = "manuals"."id"
WHERE (LOWER(manual_translations.title) LIKE '%foo%' OR
LOWER(manual_translations.content) LIKE '%foo%')
AND "manual_translations"."locale" = 'en'"
请注意,with_translations
会自动在该manual_translations.locale='en'
上添加标签,因此您只能过滤出您所在区域设置中的结果,我假设这就是您想要的结果
让我知道这是否适合你。谢谢克里斯!工作起来很有魅力!伟大的我注意到,
where
查询的复杂性超出了必要的范围,因此简化了一点(见上文)。抱歉,我弄错了,修复了它。现在应该可以了,稍微短一点。
Manual.search_query("foo")
SELECT "manuals".* FROM "manuals"
INNER JOIN "manual_translations" ON "manual_translations"."manual_id" = "manuals"."id"
WHERE (LOWER(manual_translations.title) LIKE '%foo%' OR
LOWER(manual_translations.content) LIKE '%foo%')
AND "manual_translations"."locale" = 'en'"