首页 \ 问答 \ 嵌套子查询与Ecto(Nested subqueries with Ecto)

嵌套子查询与Ecto(Nested subqueries with Ecto)

使用Ecto v2.2.6,Phoenix 1.3

我有一个带有新闻源的博客应用。 它是这样工作的:

  • 用户可以提交帖子。
  • 用户可以关注其他用户。
  • 当用户提交帖子时,会添加新闻源表格中的项目。
  • 用户可以看到他们所关注的用户提交的帖子的新闻源。

我想使用Ecto.Query来获取给定用户所关注的用户的新闻源项目列表。


快速背景。 这是对象:

用户

mix phx.gen.json Accounts User users email:string password:string

岗位

mix phx.gen.json Content Post posts title:string content:string user_id:references:users

usersposts有一个has_many:belongs_to: relationship。)

跟随

mix phx.gen.json Accounts Follow follows following_id:references:users followed_id:references:users

(当用户A跟随用户B时,会创建一个新的Follow条目,其中following_id指向A,并且followed_id指向B.)

新闻源

mix phx.gen.json Content Newsfeeditem newsfeeditems type:string, user_id:integer, content:string

现在我想查询这些东西。 为了让我能够获得给定用户的Newsfeeditems列表,这很简单:

导入Ecto.Query

query =
  from n in Newsfeeditem,
    where: n.user_id == ^user_id

假设我是用户1,我正在关注用户2,3和4. follows表中有三个条目。 要为这些用户获取所有相应的newsfeeditems,查询将如下所示:

query =
  from n in Newsfeeditem,
    where: n.user1_id in [2,3,4]

我想让它充满活力。 这是我迷路的地方。 我想做一些类似于此的事情:

subquery =
  from f in Follow,
    where: f.following_id == 1,
    select: f.follower_id

query =
  from n in Newsfeeditem,
    where: n.user_id in (Repo.all(subquery))

显然这不起作用,但我不确定如何正确地构造这些东西。

如何通过子查询选择此项? ( 注意我正在寻找特定的子查询解决方案,但如果有更好的方法则可以获得奖励积分)


Using Ecto v2.2.6, Phoenix 1.3

I have a Blog app with a Newsfeed. It works like this:

  • Users can submit Posts.
  • Users can follow other Users.
  • When a user submits a Post, an item in the Newsfeed table is added.
  • Users can see a newsfeed of Posts that have been submitted by Users they are following.

I want to use Ecto.Query to get a list of newsfeed items from users that a given user is following.


Quick background. Here are the objects:

User

mix phx.gen.json Accounts User users email:string password:string

Post

mix phx.gen.json Content Post posts title:string content:string user_id:references:users

(users and posts have a has_many: and belongs_to: relationship.)

Follow

mix phx.gen.json Accounts Follow follows following_id:references:users followed_id:references:users

(When user A follows user B, a new Follow entry is created where following_id points to A, and followed_id points to B.)

Newsfeed

mix phx.gen.json Content Newsfeeditem newsfeeditems type:string, user_id:integer, content:string

Now I want to query this stuff. For me to get a list of Newsfeeditems for a given user, it's easy:

import Ecto.Query

query =
  from n in Newsfeeditem,
    where: n.user_id == ^user_id

Let's say I'm user 1, and I am following users 2, 3 and 4. There are three entries in the follows table. To get all the corresponding newsfeeditems for those users, the query would look something like this:

query =
  from n in Newsfeeditem,
    where: n.user1_id in [2,3,4]

I want to make it dynamic. Here's where I am lost. I would like to do something that resembles this:

subquery =
  from f in Follow,
    where: f.following_id == 1,
    select: f.follower_id

query =
  from n in Newsfeeditem,
    where: n.user_id in (Repo.all(subquery))

Obviously this doesn't work, but I'm not sure how to structure these things correctly.

How can I select this via subquery? (Note I'm looking for the subquery solution specifically, but bonus points if there is a better way)


原文:https://stackoverflow.com/questions/46719696
更新时间:2022-03-31 13:03

最满意答案

而不是使用那个复杂的选择器,使用Kendo UI根据name创建的选择器:

toolbar: [
    {
        name: "Add",
        text: "Add new record",
        click: function(e){console.log("foo"); return false;}
    }
],

接着:

$(".k-grid-Add", "#grid").bind("click", function (ev) {
     // your code
     alert("Hello");
});

Instead of using that complex selector use the one that Kendo UI creates from name:

toolbar: [
    {
        name: "Add",
        text: "Add new record",
        click: function(e){console.log("foo"); return false;}
    }
],

and then:

$(".k-grid-Add", "#grid").bind("click", function (ev) {
     // your code
     alert("Hello");
});

相关问答

更多

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。