问题标签 [arel]

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 回答
431 浏览

ruby-on-rails - 复杂的arel查询

我有一个模型,其中一篇文章可以有多个标签(以及一个标签多个文章)。文章有两个子类,产品和套件。产品有一个类别,套件没有。

如何获取某个标签(我知道 tag.id)的所有文章(套件和产品),并加载产品的类别(避免 n+1)?

0 投票
3 回答
18642 浏览

ruby-on-rails - arel,如何加入

给定

使用 Rails 3 堆栈,我如何查询“拥有”产品的所有类别?

0 投票
4 回答
358 浏览

ruby-on-rails - 如何创建一个范围来查找“零帖子的作者”?

使用 Rails 3,这个范围可以按预期工作:

生成的 SQL 为:

但它的逆不返回任何结果:

我假设第三行根本没有选择零帖子的作者......那么解决方案是什么?

0 投票
1 回答
631 浏览

sql - Arel 导致聚合无限循环

我无法使用 Arel 在同一个查询中聚合 2 列。当我运行它时,整个服务器会冻结一分钟,然后 rails dev-server 崩溃。我怀疑一个无限循环:)。

也许我误解了 Arel 的整个概念,如果有人能看一下,我将不胜感激。

该查询的预期结果是这样的: [{:user_id => 1, :sum_account_charges => 300, :sum_paid_debts => 1000},...]

0 投票
2 回答
3081 浏览

sql - What just happened to Arel and what do I do with an Arel::SelectManager?

I'm desperately trying to make sense of Arel, mostly because I hate dealing with SQL; I was doing so well, but I've hit a wall.

I've been working in Rails 3.0.0, and I'm trying to make a complex query with some math in it. The real case is rather more complex, but I've simplified a bit. In my example, I have a table with a particular string field, and I want a count of all the records, as well as a count for each of two possible values of that field, grouped by a foreign id.

Under Rails 3.0.0, I can do this (console commands):

and at this point I can do do x.to_sql and... well, I'm not entirely sure it's right, but the results look right, aside from having the foreign_id column twice.

So far so good. However, when I try to join in a second set of counts like this:

it just hangs up, leading me to think I'm hitting this bug

(btw I've got more counts to add in, and ideally 'some_thingies' should itself be an arel object representing more filtering on which thingies we're counting... but I digress...)

So, I decided to try out the latest edge Arel and Rails, and bumped up my gems accordingly:

and now when I try to do do the first join, it fails miserably:

This doesn't seem like a bug in Arel - if anything, it seems more like the fact that it worked before is the bug. I think I just don't know what an Arel::SelectManager is and what to do with it. It seemed like I was doing so well, but I really don't get what is going on.

Do I need to somehow make a new table based on the SelectManager I've got? Or am doing something wrong in my configuration which makes the [] syntax fail? Or do I just totally fail to understand what Arel does? I still don't quite get what I'm supposed to do with Arel::Rows, but I suppose I'll get the hang of that; and I suspect I can get rid of the extra foreign key in the results with a project()...

But I'm still pretty lost. Haaaaalp!

p.s. does 'railties' rhyme with 'frailties,' or with 'mail guys'?

0 投票
2 回答
1163 浏览

ruby-on-rails - Rails 3 查询接口:使用关联模型

我将使用通用博客示例。

查询 Post 时,如何访问它的关联(即:comments)?

这应该是世界上最简单的事情,但我还没有找到任何关于它的文档。甚至http://edgeguides.rubyonrails.org/3_0_release_notes.html#query-interfacehttp://m.onkey.org/2010/1/22/active-record-query-interface也无济于事,基本上是说“现在有是像连接和包含这样的方法来做与 SQL 语句相同的事情。” 是的,谢谢。

所以这是我想做的非常简单的事情,它们不起作用,但我想要完成的事情应该很明显:

如果不使用带有 SQL 气味的 ruby​​ 代码(从而违背了 Active Record 的目的),我们如何才能做到这些?谢谢 :)

0 投票
1 回答
353 浏览

ruby-on-rails - 在多态树上找到一个父节点

我有一个简单的多态关联

所以在 IRB 中我可以做 Post.comments 或 Comment.comments。

但是我怎样才能找到父帖子?

就像在 Comment.post 中一样?

我目前可以通过执行一系列.commentable's 来获得它们。例如 :

0 投票
1 回答
1847 浏览

ruby-on-rails - Rails 3 ActiveRecord 查询,返回模型的所有记录,并在单个查询中包含关联记录的计数

我有以下型号:

我想要一个包含所有关键字的数组,对于每个关键字,以及用该关键字标记的研究计数,这样我就可以说:

我确信这是可能的,问题是:如何?

0 投票
3 回答
1787 浏览

sql - 使用 Arel (Rails3) 的嵌套查询

例如,我有 2 个模型:

购买 (belongs_to :users) 用户 (has_many :purchases)

我想选择至少有一次购买的所有用户。

在 SQL 中,我会这样写:

还有一个问题:是否有任何涵盖 Arel 的完整文档或书籍?

0 投票
3 回答
3438 浏览

sql - 通过 OR 或 AND 连接(胶水)条件(Arel,Rails3)

我有几个复杂的查询(使用子查询等),并希望将它们与 OR 或 AND 语句粘合在一起。

例如:

我想要类似的东西

下一个示例对我不起作用:

因为我有几个 where(..) 查询,我想连接它们

换句话说

我有 3 种方法:首先返回第一个位置,第二个第二个,第三个 - 或连接。

我必须能够在我的应用程序中使用所有 3 种方法并保存DRY代码