English 中文(简体)
当我呼吁合并时,为什么要恢复阵列?
原标题:Why is ActiveRelation returning an array when I call merge?

I have models called User, Activity, and Task and I can do the following

> puts Activity.joins(:task).to_sql
SELECT "activities".* FROM "activities" INNER JOIN "tasks" ON "tasks"."id" = "activities"."task_id" ORDER BY date DESC

> puts User.first.shared_tasks.to_sql
SELECT "tasks".* FROM "tasks" INNER JOIN "user_tasks" ON "tasks"."id" = "user_tasks"."task_id" WHERE "user_tasks"."user_id" = 1

但是,当我试图打上<条码>时,即。 我收到了两个阵列:

> puts Activity.joins(:task).merge(User.first.shared_tasks).to_sql
NoMethodError: undefined method `to_sql  for []:Array

Why is that not returning a relation? I need to put a where clause on it.

最新情况:

在进行进一步检查后,将立即对<代码>User.first. Commond_tasks进行评价。 我可以通过增加<条码><>条码/代码>来了解我所希望的行为。 呼吁:

> puts Activity.joins(:task).merge(User.first.shared_tasks.order()).to_sql
SELECT "activities".* FROM "activities" INNER JOIN "tasks" ON "tasks"."id" = "activities"."task_id" INNER JOIN "user_tasks" ON "tasks"."id" = "user_tasks"."task_id" WHERE "user_tasks"."user_id" = 1 ORDER BY date DESC

除了增加一个空洞的<条码><>条码/代码>外,是否还有更好的办法防止这一关系得到评估?

最佳回答

诚然,我仍没有说明为什么要立即评价<代码>User.first. Commond_tas,但我已经做了一些工作。 我只能打电话到<条码> 范围<>。

> User.first.shared_tasks.class
=> Array

> User.first.shared_tasks.scoped.class
=> ActiveRecord::Relation

Now when I try to do the merge:

Activity.joins(:task).merge(User.first.shared_tasks.scoped)

它使用<条码>活性记录:Relation#mer,而不是Array#mer

问题回答

Whenever you call a method on a relation object that it doesn t respond to, it delegates that method to some other type of object. In the case of merge, it will be converted to an array. This is done via the ActiveRecord::Delegation module. Some methods such as each are explicitly delegated, and others such as merge are done through method_missing.

例如,如果是:

Activity.joins(:task).each { |activity| puts activity }

联系代表<代码>.each到一个阵列(计算器),基本上进行转换。

为了与你的关系,你可以尝试这样做。

puts (Activity.joins(:task) & User.first.shared_tasks).to_sql

合并有助于形成一个范围,并归还一系列已实现的目标。





相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签