问题标签 [named-scope]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
256 浏览

ruby-on-rails - 使用 named_scope 查找关联行数(Ruby on Rails + Searchlogic 问题)

假设我有:

我应该放什么??????允许 searchlogic 查询,例如:

任何帮助将不胜感激!

0 投票
3 回答
6159 浏览

ruby-on-rails - How to filter results by multiple fields?

I am working on a survey application in ruby on rails and on the results page I want to let users filter the answers by a bunch of demographic questions I asked at the start of the survey.

For example I asked users what their gender and career was. So I was thinking of having dropdowns for gender and career. Both dropdowns would default to all but if a user selected female and marketer then my results page would so only answers from female marketers.

I think the right way of doing this is to use named_scopes where I have a named_scope for every one of my demographic questions, in this example gender and career, which would take in a sanitized value from the dropdown to use at the conditional but i'm unsure on how to dynamically create the named_scope chain since I have like 5 demographic questions and presumably some of them are going to be set to all.

0 投票
1 回答
1162 浏览

ruby-on-rails - named_scope + average 导致在 postgresql 上运行的 sql 查询中指定的表不止一次

我有一个像这样的命名范围......

当我调用它时,一切正常。

我也有我称之为的这种平均方法......

如果我这样称呼它也可以。

但是,如果我这样称呼它...

我得到...

ActiveRecord::StatementInvalid:PGError:错误:表名“profiles”指定了不止一次:SELECT avg("answers".rating) AS avg_rating,profiles.career AS profiles_career FROM "answers" LEFT OUTER JOIN "survey_sessions"survey_sessions_answers ON "survey_sessions_answers ".id = "answers".survey_session_id 左外连接 "profiles" ON "profiles".id = "survey_sessions_answers".profile_id INNER JOIN "survey_sessions" ON "survey_sessions".id = "answers".survey_session_id INNER JOIN "profiles" ON "profiles".id = "survey_sessions".profile_id WHERE ("profiles"."gender" = E'm') GROUP BY profiles.career

这有点难以阅读,但说我包含了两次表格配置文件。

如果我只是从平均值中删除包含它可以工作,但它并不实用,因为平均值实际上是在一个通过范围的方法内部调用的。因此,有时性别或平均值可能会被相互调用,如果其中任何一个丢失了配置文件,那么它就不起作用了。

所以要么我需要知道如何在 Rails 中修复这个明显的错误,要么想办法知道哪些范围应用于 ActiveRecord::NamedScope::Scope 对象,以便我可以检查它们是否已被应用,如果没有添加包括平均值。

0 投票
1 回答
138 浏览

ruby-on-rails - rails named_scope 作为 AR::Base 的扩展

我想扩展 AR::Base 类以使所有模型都有这个 named_scope,我该怎么做?

0 投票
2 回答
82 浏览

ruby-on-rails - 仅当存在参数时才执行 named_scoped

嗨有这样的模型:

如果我启动 name = "ba" start = Date.today EventDate.named(name).date_range(start , start + 2),它会正常工作

但是如果名称或开始是 nil 我不想执行 named_scope 像 name = nil EventDate.named(name).date_range(start , start + 2)

是否可以在 named_scope 内设置条件?

谢谢

0 投票
2 回答
4400 浏览

ruby-on-rails - 将 named_scope 与子模型计数一起使用

我有一个简单的父对象,有很多孩子。我试图弄清楚如何使用命名范围来带回具有特定数量孩子的父母。

这可能吗?

我希望做类似的事情Foo.with_one_bar

我可以在父类上写这样的方法,但我宁愿拥有命名范围的力量

0 投票
1 回答
626 浏览

sql - 如何将 find_by_sql 语句翻译成 named_scope?

如何将以下内容转换为 named_scope?

这是我现在拥有的,但它似乎不起作用:

最终,这是有效的;

0 投票
1 回答
137 浏览

mysql - 如何解决 MySQL 错误?链接命名范围

我试图在我的用户模型中链接两个 named_scopes。

首先:

第二:

我正在尝试像这样链接两者:

但是,我收到以下错误:

我该如何解决这个问题?

0 投票
2 回答
204 浏览

sql - 如何将此 find_by_sql 转换为 named_scope?

我怎么可能变成named_scope?

0 投票
3 回答
2422 浏览

ruby-on-rails - Rails 中命名作用域的意义是什么?

在了解详细信息之前。

问题1:--这里的scope是什么意思(即命名为**scope)?**

使用命名范围有什么好处?

现在:-

来自使用 Rails 的敏捷开发一书:-

这样一个命名范围将使找到上周的订单价值变得轻而易举。

范围也可以组合

为什么我们在这里使用 named_scope。我们可以使用方法来做同样的事情。我们使用 named_scope 得到了什么特别的东西。