如何通过空has_many关联声明where子句来检索对象?

时间:2016-09-28 06:53:55

标签: ruby-on-rails rails-activerecord

Post has_many :images
Image belongs_to :post

我想选择所有没有图片的帖子。

Post.includes(:images).where.not(images: nil) # FAIL

感谢您的帮助。

3 个答案:

答案 0 :(得分:0)

我认为您可以通过以下方式实现您的目标:

Post.includes(:images).where.not(id: Image.pluck(:post_id).uniq)

如果你想include与条件有关联,你需要创建一个与条件有关的新关联。

类似的东西:

# app/models/user.rb
class User < ActiveRecord::Base
  has_many :posts, dependent: :destroy
  has_many :published_posts, -> { published }, class_name: 'Post'
end

# app/models/post.rb
class Post < ActiveRecord::Base
  belongs_to :user
  scope :published, -> { where(published: true) }
end

答案 1 :(得分:0)

使用WITH MEMBER [Measures].[Key for Today] AS Format ( Now(),'yyyyMMdd' ) MEMBER [Measures].[Today string] AS '[Date].[Dates].[Day].&[' + [Measures].[Key for Today] + ']' MEMBER [Measures].[Quantity Shipped MTD] AS Sum ( MTD([Date].[Dates].CurrentMember) ,[Measures].[Quantity Shipped] ) MEMBER [Measures].[Quantity Shipped YTD] AS Sum ( YTD([Date].[Dates].CurrentMember) ,[Measures].[Quantity Shipped] ) MEMBER [Measures].[Sales Amount MTD] AS Sum ( MTD([Date].[Dates].CurrentMember) ,[Measures].[Sales Amount] ) MEMBER [Measures].[Sales Amount YTD] AS Sum ( YTD([Date].[Dates].CurrentMember) ,[Measures].[Sales Amount] ) MEMBER [Measures].[Cost Amount MTD] AS Sum ( MTD([Date].[Dates].CurrentMember) ,[Measures].[Cost Amount - Sales] ) MEMBER [Measures].[Cost Amount YTD] AS Sum ( YTD([Date].[Dates].CurrentMember) ,[Measures].[Cost Amount - Sales] ) MEMBER [Measures].[Sales Margin MTD] AS Sum ( MTD([Date].[Dates].CurrentMember) ,[Measures].[Sales Margin] ) MEMBER [Measures].[Sales Margin YTD] AS Sum ( YTD([Date].[Dates].CurrentMember) ,[Measures].[Sales Margin] ) MEMBER [Measures].[Forecast Quantity MTD] AS Sum ( MTD([Date].[Dates].CurrentMember) ,[Measures].[Forecast Quantity] ) MEMBER [Measures].[Forecast Quantity YTD] AS Sum ( YTD([Date].[Dates].CurrentMember) ,[Measures].[Forecast Quantity] ) MEMBER [Measures].[Forecast Turnover MTD] AS Sum ( MTD([Date].[Dates].CurrentMember) ,[Measures].[Forecast Total Turnover] ) MEMBER [Measures].[Forecast Turnover YTD] AS Sum ( YTD([Date].[Dates].CurrentMember) ,[Measures].[Forecast Total Turnover] ) MEMBER [Measures].[Forcast Cost MTD] AS Sum ( MTD([Date].[Dates].CurrentMember) ,[Measures].[Forecast Total Cost] ) MEMBER [Measures].[Forecast Cost YTD] AS Sum ( YTD([Date].[Dates].CurrentMember) ,[Measures].[Forecast Total Cost] ) MEMBER [Measures].[Forecast Margin MTD] AS Sum ( MTD([Date].[Dates].CurrentMember) ,[Measures].[Forecast Margin] ) MEMBER [Measures].[Forecast Margin YTD] AS Sum ( YTD([Date].[Dates].CurrentMember) ,[Measures].[Forecast Margin] ) MEMBER [Measures].[Budget Quantity MTD] AS Sum ( MTD([Date].[Dates].CurrentMember) ,[Measures].[Budget Quantity] ) MEMBER [Measures].[Budget Quantity YTD] AS Sum ( YTD([Date].[Dates].CurrentMember) ,[Measures].[Budget Quantity] ) MEMBER [Measures].[Budget Turnover MTD] AS Sum ( MTD([Date].[Dates].CurrentMember) ,[Measures].[Budget Total Turnover] ) MEMBER [Measures].[Budget Turnover YTD] AS Sum ( YTD([Date].[Dates].CurrentMember) ,[Measures].[Budget Total Turnover] ) MEMBER [Measures].[Budget Cost MTD] AS Sum ( MTD([Date].[Dates].CurrentMember) ,[Measures].[Budget Total Cost] ) MEMBER [Measures].[Budget Cost YTD] AS Sum ( YTD([Date].[Dates].CurrentMember) ,[Measures].[Budget Total Cost] ) MEMBER [Measures].[Budget Margin MTD] AS Sum ( MTD([Date].[Dates].CurrentMember) ,[Measures].[Budget Margin] ) MEMBER [Measures].[Budget Margin YTD] AS Sum ( YTD([Date].[Dates].CurrentMember) ,[Measures].[Budget Margin] ) SELECT { [Measures].[Quantity Shipped MTD], [Measures].[Sales Amount MTD], [Measures].[Cost Amount MTD], [Measures].[Sales Margin MTD], [Measures].[Margin %], [Measures].[Forecast Quantity MTD], [Measures].[Forecast Turnover MTD], [Measures].[Forcast Cost MTD], [Measures].[Forecast Margin MTD], [Measures].[Margin %], [Measures].[Budget Quantity MTD], [Measures].[Budget Turnover MTD], [Measures].[Budget Cost MTD], [Measures].[Budget Margin MTD], [Measures].[Margin %], [Measures].[Quantity Shipped YTD], [Measures].[Sales Amount YTD], [Measures].[Cost Amount YTD], [Measures].[Sales Margin YTD], [Measures].[Margin %], [Measures].[Forecast Quantity YTD], [Measures].[Forecast Turnover YTD], [Measures].[Forecast Cost YTD], [Measures].[Forecast Margin YTD], [Measures].[Margin %], [Measures].[Budget Quantity YTD], [Measures].[Budget Turnover YTD], [Measures].[Budget Cost YTD], [Measures].[Budget Margin YTD], [Measures].[Margin %] } ON COLUMNS, NON EMPTY { [Customer].[Business Type].[Business Type].ALLMEMBERS* [Customer Sales].[Summary Prod Group 1].[Summary Prod Group 1].ALLMEMBERS} On Rows From ( SELECT -{ [Customer].[Customer].&[5637] ,[Customer].[Customer].&[4125] ,[Customer].[Customer].&[1727] } ON 0 ,{[Customer].[Business Type].&[Export]} ON 1 ,- { [Customer Sales].[Summary Prod Group 1].&[Missing Product Group] } ON 2 From [Sales]) Where StrToMember ( [Measures].[Today string] ,constrained )

尝试此操作
includes

请注意,这将首先获取所有帖子,然后对其进行迭代。它不是数据库在这里为你做的主要工作,相反,ruby / activerecord为每个帖子创建一个对象,然后查询帖子。

答案 2 :(得分:0)

  

我想选择所有没有图片的帖子

你与where.not事物很接近。

以下是实现它的传统方式:

Post.includes(:images).where(images: { id: nil })