我有用户模型,我需要创建一个查询,如下所示
SELECT DISTINCT u.id FROM users as u
LEFT JOIN lists_users as lu ON lu.user_id = u.id
WHERE u.id != 3
AND lu.list_id != 34
我试着
scope :include_current_user, lambda { |user, list|
joins('LEFT JOIN lists_users AS lu ON lu.user_id = id')
.where('id != :user_id AND lu.
我有一个Author模型,其中habtm :feed。在使用Rails3时,希望设置一个范围来查找没有关联提要的所有作者。
class Author < ActiveRecord::Base
has_and_belongs_to_many :feeds
scope :without_feed, joins(:feeds).where("authors_feeds.feed_id is null")
end
...似乎不起作用.这感觉像是一件简单的事情。这里我漏掉了什么?
我觉得很傻,但我找不到答案。
如果我有一个拥有许多属性的用户,给出一个属性列表,我可以这样问rails:
User.where("attributes.id IN ?", list_of_attribute_ids)
与适当的联接或包括或其他任何东西。
但是,我不知道如何找到这些用户的逆集。也就是说,给定100个用户,如果结果返回75条条目,我不知道如何找到另外的25条!
我以为
User.where("attributes.id NOT IN ?", list_of_attribute_ids)
可能会起作用(类似于User.where.not),但它不起作用
我试图过滤特定用户在讨论中的最后一次活动,所以如果用户发表了一篇文章或评论,这个讨论将成为他们个人列表中的第一个。
关系:
a has_many has_many .a Post has_many注释.a User has_many through .通过帖子进行讨论.<code>H 210</code><code>F 211</code>
我的查询基本上是有效的,但只有在讨论中同时存在来自用户的帖子和评论的情况下。
我如何调整我的查询以查找last_created帖子或last_created注释,意思是在我的.order中,如果它是空的,并且
在下面的场景中,如何计算票证的数量?ticket列位于Items表中。 我正在尝试抓取一组赎回,然后计算作为这组赎回的“门票”的物品数量。 class Item < ActiveRecord::Base
has_many :redemptions
class Redemption < ActiveRecord::Base
belongs_to :item #This method works, but is there a much better way?
def tickets_sold
my_tickets_sold = 0
我使用的是rails 5。当我在rails控制台中执行以下直接SQL查询时,我得到了以下预期结果:
2.3.5 :053 > w = ActiveRecord::Base.connection.exec_query("SELECT * FROM students s LEFT JOIN (SELECT student_id, MAX(urgent) AS has_urgent,MAX(created_at) AS last_contact FROM reports GROUP BY student_id) r ON r.student_id = s.id")
class User < ActiveRecord::Base
has_many :roles
has_many :allocations, class_name: 'Project', inverse_of: :user
end
class Role < ActiveRecord::Base
belongs_to :user
end
class Project < ActiveRecord::Base
belongs_to :user, inverse_of: :allocations
end
我想要找到那些没有分配的用户,以及分配的star
我有以下模型: class User < ApplicationRecord
has_and_belongs_to_many :lists
end class Workspace < ApplicationRecord
has_many :lists, dependent: :nullify
end class List < ApplicationRecord
has_and_belongs_to_many :users
belongs_to :workspace, optional: true
scope :by_workspace, ->(w
我在Rails中尽量避免在joins中插入字符串,因为我注意到在将查询器链接在一起时灵活性会降低。也就是说,我觉得joins(:table1)比joins('inner join table1 on table1.id = this_table.table1_id')灵活得多。
我想要完成的是:
FROM table1
INNER JOIN table2 on table2.id = table1.table2_id
LEFT JOIN table3 on table3.id = table2.table3_id
内部连接全部
但是,我不知道如何使用Rails术语来实现:
Ta
我有两个模特:
class User < ActiveRecord::Base
has_and_belongs_to_many :groups
end
class Group < ActiveRecord::Base
has_and_belongs_to_many :users
end
我想要制作一个作用域(为了效率和连锁作用域的能力),返回不属于任何组的用户。经过多次尝试,我未能执行一种方法而不是范围,这使得collect在User.all上变得丑陋和..。不对。
有什么帮助吗?
也许对于第二个问题:我成功地创建了一个范围,返回属于任何给定组的用户(作为id的数
假设我有ActiveRecord模型,比如User has_one :photo。在数据库中,photos有一个t.binary列,该列可能包含大量数据,因此,除非需要,否则我不想对该列进行SELECT。
我想做这样的事情:
users.each do |user|
image_tag(user_photo_path) if user.photo.present?
end
但是,我不想打电话给user.photo.present?,因为:
这样做会加载photo关联,包括SELECT * from photos
即使它可以仅用于SELECT id FROM photos来检查是否存
我正在我的Ruby on Rails应用程序中编写单元和功能测试。每当有人打开针对我的缺陷时,我都会尝试编写一个测试。最近出现的一个缺陷与我们如何更新对象的缓存或使其无效直接相关。在编写测试时考虑缓存是正常的吗?我还没有看到它做的太多,但这些天有太多的东西存储在诸如Redis或Memcached (我们使用Redis)这样的工具中。我认为对从这些内存存储中存储/检索的任何东西的测试也需要在测试中进行。在Rails 3中,有没有什么工具或建议可以帮助你做到这一点?