Rails 4 找到缺少某些子关联的父关联
Rails 4 find parent associations which are missing certain child associations
我正在尝试查询哪些父(类别)记录没有具有给定 属性(author.id)的 ha 子记录(post)。
class Category < ActiveRecord::Base
has_many :posts, dependent: :destroy
end
class Post < ActiveRecord::Base
belong_to :category
end
class Author < ActiveRecord::Base
has_many :posts, dependent: :destroy
end
我正在尝试查找作者从未 post 编辑过哪些类别。我的想法是获取一个 GIVEN 作者没有 post 的类别数组,他们可以还有其他 posts...只是不是特定作者
@author_unused_categories = @author.unused_categories
class Author < ActiveRecord::Base
def unused_categories
categories = Categories.all
author_used_categories = Post.where(author_id: self.id).select(:category).uniq ??????
end
end
categories = Author.includes(posts: :category).select('category.id AS category_id').map(&:category_id)
unused_categories = Category.where.not(id: categories)
我正在尝试查询哪些父(类别)记录没有具有给定 属性(author.id)的 ha 子记录(post)。
class Category < ActiveRecord::Base
has_many :posts, dependent: :destroy
end
class Post < ActiveRecord::Base
belong_to :category
end
class Author < ActiveRecord::Base
has_many :posts, dependent: :destroy
end
我正在尝试查找作者从未 post 编辑过哪些类别。我的想法是获取一个 GIVEN 作者没有 post 的类别数组,他们可以还有其他 posts...只是不是特定作者
@author_unused_categories = @author.unused_categories
class Author < ActiveRecord::Base
def unused_categories
categories = Categories.all
author_used_categories = Post.where(author_id: self.id).select(:category).uniq ??????
end
end
categories = Author.includes(posts: :category).select('category.id AS category_id').map(&:category_id)
unused_categories = Category.where.not(id: categories)