使用带参数的范围和与ActiveRecord(Ruby)的has_and_belongs_to_many关联(using scopes with parameters and has_and_belongs_to_many associations with ActiveRecord (Ruby))
在我的
Sinatra
应用程序中,我的模型定义了Users
和Notifications
之间的HABTM关系。 我正在尝试定义几个范围,一个用于与没有Users
称为unread
所有Notifications
,另一个用于返回特定User
“未读”的所有Notifications
。class Notification < ActiveRecord::Base has_and_belongs_to_many :users scope :unread, ->{ Notification.joins("LEFT JOIN notifications_users ON notifications.id = notifications_users.notification_id"). where("notifications_users.user_id IS NULL").uniq } scope :unread_by, ->(u){ Notification.joins("LEFT JOIN notifications_users ON notifications.id = notifications_users.notification_id"). where("notifications_users.user_id <> ?", u.id).uniq }
unread
范围工作正常但未unread_by
范围并未给出我期望的结果。it "should know which notifications have not yet been read by anyone, or by a particular user" do n1 = Notification.create!(title: 'test 1', text: 'this is some longer text about the notification') n2 = Notification.create!(title: 'test 2', text: 'this is also some longer text about the notification') Notification.unread.must_include(n1) Notification.unread.must_include(n2) @user1.read(n1) Notification.unread.wont_include(n1) Notification.unread.must_include(n2) Notification.unread_by(@user1).wont_include(n1) Notification.unread_by(@user1).must_include(n2) # => fails Notification.unread_by(@user2).must_include(n1) Notification.unread_by(@user2).must_include(n2) # => fails end
我怀疑我的查询逻辑是有缺陷的但是我已经盯着这个太久了,我只是没有看到它。 我错过了什么?
In my
Sinatra
app my model defines a HABTM relationship betweenUsers
andNotifications
. I am trying to define a couple of scopes, one for allNotifications
associated with noUsers
calledunread
and one that returns allNotifications
that are 'unread' by a particularUser
.class Notification < ActiveRecord::Base has_and_belongs_to_many :users scope :unread, ->{ Notification.joins("LEFT JOIN notifications_users ON notifications.id = notifications_users.notification_id"). where("notifications_users.user_id IS NULL").uniq } scope :unread_by, ->(u){ Notification.joins("LEFT JOIN notifications_users ON notifications.id = notifications_users.notification_id"). where("notifications_users.user_id <> ?", u.id).uniq }
The
unread
scope works fine but theunread_by
scope is not giving me the results I expect.it "should know which notifications have not yet been read by anyone, or by a particular user" do n1 = Notification.create!(title: 'test 1', text: 'this is some longer text about the notification') n2 = Notification.create!(title: 'test 2', text: 'this is also some longer text about the notification') Notification.unread.must_include(n1) Notification.unread.must_include(n2) @user1.read(n1) Notification.unread.wont_include(n1) Notification.unread.must_include(n2) Notification.unread_by(@user1).wont_include(n1) Notification.unread_by(@user1).must_include(n2) # => fails Notification.unread_by(@user2).must_include(n1) Notification.unread_by(@user2).must_include(n2) # => fails end
I suspect my query logic is flawed but I've been staring at this for too long and I'm just not seeing it. What am I missing?
原文:https://stackoverflow.com/questions/18629846
最满意答案
match(c("R","A"),LETTERS) [1] 18 1
match(c("R","A"),LETTERS) [1] 18 1
相关问答
更多-
OrderedDict将保留它有权访问的任何订单。 将订单数据传递给它进行初始化的唯一方法是传递键值对的列表(或更一般地,可迭代的),如最后两个示例所示。 正如您链接的文档所述,OrderedDict在传递关键字参数或dict参数时无法访问任何顺序,因为在OrderedDict构造函数看到之前,已删除任何订单。 请注意,在上一个示例中使用列表推导不会改变任何内容。 OrderedDict([(i,i) for i in l])和OrderedDict([('b', 'b'), ('a', 'a'), ('c ...
-
如何以稳定的顺序查找数组中max n个元素的索引(How to find indices of max n elements in array in stable order)[2021-08-30]
码 def max_with_order(arr, n) arr.each_with_index.max_by(n) { |x,i| [x,-i] }.map(&:last) end 例子 a = [0,1,2,3,3,4] max_with_order(a, 1) #=> [5] max_with_order(a, 2) #=> [5, 3] max_with_order(a, 3) #=> [5, 3, 4] max_with_order(a, 4) #=> [5, 3, 4, 2] ... -
第一行的顶点顺序实际上是 0 4 1 5 2 6 3 7 然后继续 7 11 6 10 5 9 4 8 8 12 9 13 10 14 11 15 剔除三角形条带会反转每个三角形。 您必须将7和8放入两次的原因是,在这些点处,剔除实际上不应该被颠倒。 实现这一目标的唯一可能性是通过反转剔除两次(实际渲染一个不可见的多边形),以便继续使用与之前相同的剔除方向。 The vertex order for the first row would actually be 0 4 1 5 2 6 3 7 a ...
-
你应该使用std :: stable_sort 。 在你的具体情况中,当val[a] == val[b]时,你可以调整lambda来比较a和b ,但这会让你的意图模糊不清将来会遇到的任何未来开发者。 You should use std::stable_sort. In your specific case, you could tweak the lambda to compare a with b when val[a] == val[b] but that would make your intent ...
-
match(c("R","A"),LETTERS) [1] 18 1 match(c("R","A"),LETTERS) [1] 18 1
-
将值与struct索引相结合,对它们进行排序,并将值与值一起打印: struct ind_val { int index; double value; }; int sorting_ind_val (const void * a, const void * b) { double lhs = ((struct ind_val*)a)->value; double rhs = ((struct ind_val*)b)->value; if (lhs < rhs) ...
-
MySQL索引和按子句排序(MySQL Indices and Order By Clause)[2023-09-10]
你真的只有13排吗? 数据库可能决定简单地对它们进行排序比通过索引更快。 Do you really only have 13 rows? The database may be deciding that simply sorting them is quicker than going via the index. -
下面的示例显示了一个名为reorderfail()的重新排序的非工作版本,后跟一个名为reorder()的工作版本。 这两个函数都将vI返回到原始状态0到vA.size() - 1,但是reorderfail()无法正确重新排序vA,因为它缺少“unpermute”vA所需的间接级别。 #include
#include using namespace std; template void reorderfail(vector & vA, ... -
根据索引和内部列表的长度对嵌套列表进行分类(Categorizing a nested list based on the indices and the length of inner lists)[2022-10-06]
这里有一个示例,它生成一个以子列表长度为键的dict ,以及具有所述长度的所有子列表的列表: a= [[1,2,3],[4,5,6],[3,4],[2,1],[5,6,7],[1,3,5]] lengths = set([len(l) for l in a]) result = { n : [l for l in a if len(l) == n] for n in lengths} print(result) 这给出了: {2: [[3, 4], [2, 1]], 3: [[1, 2, 3], [4 ... -
重要的是要知道MySQL每个表和子查询最多只能使用一个索引(用于搜索,过滤和排序)(因此explain基本上每行一次),因此这里只能使用一个索引。 对于第一个查询,索引(A,B)将允许MySQL进行范围扫描并使用该顺序。 如果使用(A,B,C) ,则列C不能在范围条件中使用(因为B已经是一个范围),但是MySQL将节省读取实际tabledata的时间以获取C的值来检查最后的条件。 所以(A,B,C)通常是这里最快的选择。 “通常”,因为你当然可以有一个数据分布,其中另一个索引是最好的:如果你只有一行或两行匹 ...