Arel:来自Arel :: SelectManager的活动关系,具有联接 [英] Arel: active relation from Arel::SelectManager with join

查看:95
本文介绍了Arel:来自Arel :: SelectManager的活动关系,具有联接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们拥有一个Rails 4.2.x应用程序,并且有两个表格的帖子和作者,并且我们想使用Arel来获取由作者== Karl创作的帖子。
(在这种情况下,我们可以对Active Record联接感到满意,但这只是为了使示例简单。)

Let us we have a Rails 4.2.x app and we have two tables posts and authors, and we want to use Arel to get the posts authored by an author with name == 'Karl'. (In this case we could be happy with Active Record joins but this is just to keep the example simple.)

posts = Arel::Table.new :posts
authors = Arel::Table.new :authors

my_query = posts.project(Arel.star)
                .join(authors)
                .on(posts[:author_id].eq(authors[:id]))
                .where(authors[:name].eq('Karl'))

> my_query.class
=> Arel::SelectManager

现在,我们可以通过执行以下操作来返回帖子数组(属于Array类):

Now we could get back an array (of class Array) of posts by doing:

> Post.find_by_sql my_query
[master]  Post Load (3.1ms)  SELECT * FROM "posts" INNER JOIN "authors"
                             ON "posts"."author_id" = "authors"."id"
                             WHERE "authors"."name" = 'Karl'

=> [#<Post:0x005612815ebdf8
    id: 7474,
    ...
   ]

因此,我们确实得到了一系列帖子,而不是活动记录关系:

So we do get an array of posts, not an active record relation:

 > Post.find_by_sql(my_query).class
 => Array

也将管理器注入Post。在那里不起作用

Also injecting the manager into Post.where won't work

> Post.where my_query
=> #<Post::ActiveRecord_Relation:0x2b13cdc957bc>
> Post.where(my_query).first
ActiveRecord::StatementInvalid: PG::SyntaxError:
ERROR:  subquery must return only one column
SELECT  "posts".* FROM "posts"
WHERE ((SELECT * FROM "posts" INNER JOIN "authors" ON "posts"."author_id" = "authors"."id" WHERE "authors"."name" = 'Karel'))
ORDER BY "posts"."id" ASC LIMIT 1

我想我一定会丢失一些东西。简而言之:如何从上面的my_query这样的选择管理器(或另一个完成相同任务的选择管理器)中获得活动记录关系。

I am thinking I must be missing something. In short: how do you get an active record relation from a select manager like my_query above (or another select manager accomplishing the same thing).

推荐答案

您既无法从Arel :: SelectManager获取ActiveRecord :: Relation,也无法从sql字符串获取。您可以通过两种方式通过ActiveRecord加载数据:

You can't get ActiveRecord::Relation from Arel::SelectManager neither from sql string. You have two ways to load data through ActiveRecord:


  1. 在Arel中执行所有查询逻辑。在这种情况下,您不能使用任何ActiveRecord :: Relation方法。但是您在Arel中具有相同的功能。在您的示例中,您可以通过Arel设置限制:

  1. Do all query logic in Arel. In this case you can't use any of ActiveRecord::Relation methods. But you have same functionality in Arel. In your example you may set limit through Arel:

my_query.take(10)


  • 其他方法是在ActiveRecord :: Relation方法中使用Arel。您可以这样重写查询:

  • Other way is to use Arel in ActiveRecord::Relation methods. You may rewrite your query like this:

    posts = Arel::Table.new :posts
    authors = Arel::Table.new :authors
    
    join = posts.join(authors).
              on(posts[:author_id].eq(authors[:id])).
              join_sources
    
    my_query = Post.
                  joins(join).
                  where(authors[:name].eq('Karl'))
    
    > my_query.class
    => ActiveRecord::Relation
    


  • 在这种情况下,您可以使用 my_query 作为ActiveRecord :: Relation

    In this case you may use my_query as ActiveRecord::Relation

    这篇关于Arel:来自Arel :: SelectManager的活动关系,具有联接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆