如何发出 ActiveRecord 请求以获取其他几个项目共有的项目
How to make an ActiveRecord request to get an item common to several other items
我正在尝试修改 Sharetribe,Ruby on Rails 在线社区框架。这种方法 return 为我提供了相关的搜索过滤器。
现在,如果它出现在任何一个类别中(由 category_ids
标识),它 return 就是我的过滤器。
我希望它成为 return 过滤器,当且仅当它出现在 category_ids
识别的所有类别中时。
作为 Rails 和 ActiveRecord 的新手,我有点迷茫。这是 returning 相关过滤器的方法:
# Database select for "relevant" filters based on the `category_ids`
#
# If `category_ids` is present, returns only filter that belong to
# one of the given categories. Otherwise returns all filters.
#
def select_relevant_filters(category_ids)
relevant_filters =
if category_ids.present?
@current_community
.custom_fields
.joins(:category_custom_fields)
.where("category_custom_fields.category_id": category_ids, search_filter: true)
.distinct
else
@current_community
.custom_fields.where(search_filter: true)
end
relevant_filters.sort
end
有没有办法更改 SQL 请求,或者我应该像现在这样检索所有字段,然后删除我不感兴趣的字段?
尝试以下方法
def select_relevant_filters_if_all(category_ids)
relevant_filters =
if category_ids.present?
@current_community
.custom_fields
.joins(:category_custom_fields)
.where("category_custom_fields.category_id": category_ids, search_filter: true)
.group("category_custom_fields.id")
.having("count(category_custom_fields.id)=?", category_ids.count)
.distinct
else
@current_community
.custom_fields.where(search_filter: true)
end
relevant_filters.sort
end
这是你的HomeController
中的一个新方法,注意名称不同,只是为了省略monkeypatching。欢迎评论。
所以我通过 selecting 过滤器解决了我的问题,这些过滤器在 selected 类别的所有子类别中都存在。为此,我 select 所有子类别的所有过滤器,并且只保留返回次数完全等于子类别数的过滤器。
all_relevant_filters = select_relevant_filters(m_selected_category.own_and_subcategory_ids.or_nil)
nb_sub_category = m_selected_category.subcategory_ids.size
if nb_sub_category.none?
relevant_filters = all_relevant_filters
else
relevant_filters = all_relevant_filters.select{ |e| all_relevant_filters.count(e) == nb_sub_category.get }.uniq
end
我正在尝试修改 Sharetribe,Ruby on Rails 在线社区框架。这种方法 return 为我提供了相关的搜索过滤器。
现在,如果它出现在任何一个类别中(由 category_ids
标识),它 return 就是我的过滤器。
我希望它成为 return 过滤器,当且仅当它出现在 category_ids
识别的所有类别中时。
作为 Rails 和 ActiveRecord 的新手,我有点迷茫。这是 returning 相关过滤器的方法:
# Database select for "relevant" filters based on the `category_ids`
#
# If `category_ids` is present, returns only filter that belong to
# one of the given categories. Otherwise returns all filters.
#
def select_relevant_filters(category_ids)
relevant_filters =
if category_ids.present?
@current_community
.custom_fields
.joins(:category_custom_fields)
.where("category_custom_fields.category_id": category_ids, search_filter: true)
.distinct
else
@current_community
.custom_fields.where(search_filter: true)
end
relevant_filters.sort
end
有没有办法更改 SQL 请求,或者我应该像现在这样检索所有字段,然后删除我不感兴趣的字段?
尝试以下方法
def select_relevant_filters_if_all(category_ids)
relevant_filters =
if category_ids.present?
@current_community
.custom_fields
.joins(:category_custom_fields)
.where("category_custom_fields.category_id": category_ids, search_filter: true)
.group("category_custom_fields.id")
.having("count(category_custom_fields.id)=?", category_ids.count)
.distinct
else
@current_community
.custom_fields.where(search_filter: true)
end
relevant_filters.sort
end
这是你的HomeController
中的一个新方法,注意名称不同,只是为了省略monkeypatching。欢迎评论。
所以我通过 selecting 过滤器解决了我的问题,这些过滤器在 selected 类别的所有子类别中都存在。为此,我 select 所有子类别的所有过滤器,并且只保留返回次数完全等于子类别数的过滤器。
all_relevant_filters = select_relevant_filters(m_selected_category.own_and_subcategory_ids.or_nil)
nb_sub_category = m_selected_category.subcategory_ids.size
if nb_sub_category.none?
relevant_filters = all_relevant_filters
else
relevant_filters = all_relevant_filters.select{ |e| all_relevant_filters.count(e) == nb_sub_category.get }.uniq
end