嵌套子查询与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
(
users
和posts
有一个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
andposts
have ahas_many:
andbelongs_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 wherefollowing_id
points to A, andfollowed_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
最满意答案
而不是使用那个复杂的选择器,使用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"); });
相关问答
更多-
在这个例子中,只需使用CSS样式, #grid_printbutton { /*Styles to match other button themes */} 这将为你的按钮设计风格。 根据按钮的宽度/高度,您还可以深入了解网格的CSS样式默认值,以确保网格/寻呼机区域有足够的空间来显示这些较大的按钮。 对于预建按钮,它们的样式由这很有趣。 用于Editor的DropDownList实际上是一个SelectBox 。 这意味着我不得不使用.data('kendoSelectBox') 。 这是一个小提琴 。 这是代码: $(function () { var $log = $('#log'), fontDDL, isOpen = false; $("#editor").kendoEditor({ select: function (e) { $(".k-editor-tool ...
在Kendo网格中的工具栏上的Kendo ui-Menu中循环复选框(Loop through Checkboxes in Kendo ui-Menu on Toolbar in Kendo Grid)[2022-12-24]
首先,您必须找到checkbox元素,然后您可以使用.prop("checked")方法获取复选框选中的值。 所以,如果你,例如。 想要在打开的菜单上切换复选框值,您可以使用: $scope.onOpen = function(e) { var checkboxes = $(e.item).find(".k-item:not(:has(.k-group))").find("input[type='checkbox']"); for(var i = 0; i < checkboxes.leng ...Kendo ui grid使用工具栏中的自定义上传按钮复制记录(Kendo ui grid duplicate records with custom upload button in toolbar)[2023-07-16]
当您关闭窗口时,必须销毁其中的所有窗口小部件,以避免重复的实例。 http://docs.telerik.com/kendo-ui/intro/widget-basics/destroy 希望这有帮助 When you close you window you must destroy all widgets inside, to avoid duplicated instances. http://docs.telerik.com/kendo-ui/intro/widget-basics/destroy ...我看到两种可能的选择: 定义一个控制元素min-height的样式。 请勿删除最新元素并保留元素,但将其标记为已禁用。 解决方案1:只需定义以下样式: #toolbar .k-dropdown-wrap { min-height: 2em; } 你可以在这里看到这种方法: http : //jsfiddle.net/OnaBai/jr8mnnxL/19/ 解决方案2:不要删除最新元素但禁用下拉列表: $("#toolbar").kendoToolBar({ items: [ ...添加以下CSS样式: .k-grid-toolbar a { float:right; } 在此处查看: http : //jsfiddle.net/OnaBai/W42wK/ Add the following CSS style: .k-grid-toolbar a { float:right; } See it here: http://jsfiddle.net/OnaBai/W42wK/这是根据Kendo UI 2015年第3季度修正的错误 This was a bug fixed in Kendo UI Q3 2015 per thisKendo UI Web尚未包含此类小部件。 这是我们未来计划的一部分。 Kendo UI Web doesn't include such a widget yet. This is part of our future plans.Kendo UI工具栏按钮(Kendo UI toolbar buttons)[2023-08-20]
而不是使用那个复杂的选择器,使用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 cod ...标识Kendo Grid工具栏的类是k-grid-toolbar 。 所以为了造型,你可以使用: #grid .k-grid-toolbar { background: red; } 要将某些内容添加到工具栏,您可以使用: $(".k-grid-toolbar", "#grid").prepend("hello
"); 要么 $(".k-grid-toolbar", "#grid").before("hello
"); $(".k-grid-toolbar", ...相关文章
更多- 怎么递归json嵌套json?
- spring 嵌套事务 的 加锁 问题
- EL 表达式 嵌套问题
- ExtJS3.2,在FormPanel中嵌套TabPanel和GridPanel后无法正常显示
- 查询的时候事物的问题
- hibernate查询
- 一个sql查询
- solr join查询
- linux查询命令问题
- MongoDB学习 (六):查询
最新问答
更多- 获取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的基本操作命令。。。