这似乎相当简单,但我不能让它出现在Google上.
如果我有:
class City < ActiveRecord::Base
has_many :photos
end
class Photo < ActiveRecord::Base
belongs_to :city
end
Run Code Online (Sandbox Code Playgroud)
我想找到所有没有照片的城市.我很乐意打电话给...
City.where( photos.empty? )
Run Code Online (Sandbox Code Playgroud)
......但那不存在.那么,你如何做这种查询?
更新: 现在找到原始问题的答案,我很好奇,你如何构建逆?
IE:如果我想创建这些作为范围:
scope :without_photos, includes(:photos).where( :photos => {:city_id=>nil} )
scope :with_photos, ???
Run Code Online (Sandbox Code Playgroud) 我想在单个SQL查询中查询所有没有评论的用户?
楷模:
class User < ActiveRecord::Base
has_many :comments
end
class Comment < ActiveRecord::Base
belongs_to :user
end
Run Code Online (Sandbox Code Playgroud)
所以我想要与此相反:
User.joins.(:comments).group('users.id')
Run Code Online (Sandbox Code Playgroud)
但不是这样的:(因为它生成两个查询)
User.where.not(id: Comment.pluck(:user_id))
Run Code Online (Sandbox Code Playgroud)
也许是这样的?
User.joins.not.(:comments).group('users.id')
Run Code Online (Sandbox Code Playgroud)
感谢您的任何意见!
使用Rails 3.2.9
我试图获取与没有所有者的组织关联的项目列表.
我能够使用下面的数据列表,但对我来说似乎很难看.有一个更好的方法吗?
Items.all(:select => "items.id, items.name",
:joins => "INNER JOIN organizations on items.organization_id = organizations.id",
:conditions => "NOT EXISTS (select * from items k JOIN items_owners on items.id = items_owners.item_id) and items.organization_id = 1")
Run Code Online (Sandbox Code Playgroud)
表设置:
所有者:
项目:
items_owners:
组织:
楷模:
class Organization < ActiveRecord::Base
attr_accessible :name
has_many :items
end
class Item < ActiveRecord::Base
attr_accessible :description, :name, :owner_ids, :organization_id
has_many :items_owner
has_many :owners, :through => :items_owner
belongs_to :organization
end
class …Run Code Online (Sandbox Code Playgroud) 这应该是一个简单的查询,但我遇到了正确的Rails语法问题.我正在使用Rails 4.1.1和Postgresql(9.3).我有一个模型用户和模型公司.用户有一家公司,公司有很多用户.我正在努力寻找拥有超过5个用户的所有公司.
class Company < ActiveRecord::Base
has_many :users, dependent: :destroy
...
class User < ActiveRecord::Base
belongs_to :company
...
Run Code Online (Sandbox Code Playgroud)
问题与此类似:查找计数大于零的所有记录
如果我尝试上面提到的类似解决方案:
Company.joins(:users).group("company.id").having("count(users.id)>5")
Run Code Online (Sandbox Code Playgroud)
它给了我一个错误:
PG::UndefinedTable: ERROR: missing FROM-clause entry for table "company"
LINE 1: ... "users"."company_id" = "companies"."id" GROUP BY company.id...
Run Code Online (Sandbox Code Playgroud)
我已经尝试了几个不同的查询来获得结果,但我没有这样做.我可以使用SQL,但它似乎很愚蠢,因为这应该可以使用ActiveRecord轻松实现.
感谢所有的答复 :)
我有以下型号:
class Property < ActiveRecord::Base
has_many :photos
scope :no_photos, -> { where('properties.id NOT IN (SELECT DISTINCT(property_id) FROM photos)') }
end
class Photo < ActiveRecord::Base
belongs_to :property
end
Run Code Online (Sandbox Code Playgroud)
我知道我的范围非常低效.我需要另一种方法来获取没有任何相关照片的属性.
有帮助吗?
我有2个模型:Sophead和SopheadIssue。
SopheadIssue belongs to Sophead,
Sophead has many SopheadIssues (可选的)。
我想在Sophead模型上为与2个条件中的EITHER匹配的Sopheads创建范围:
目前,我尝试了以下方法:
scope :no_issue, -> { joins(:sophead_issues).where.not("active = ?", true) }
Run Code Online (Sandbox Code Playgroud)
但这不起作用,因为它缺少没有任何SopheadIssues的Sophead。
任何帮助,不胜感激。
非常感谢!