- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有 3 个 Ruby on Rails 表:用户、组和帖子(加上用于 has_and_belongs_to_many 的联接表“groups_users”)......
以下是相关的表定义:
create_table "posts", force: true do |t|
t.integer "user_id"
t.integer "group_id"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "groups", force: true do |t|
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "users", force: true do |t|
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "groups_users", id: false, force: true do |t|
t.integer "user_id", null: false
t.integer "group_id", null: false
end
以及模型:
Group Model:
has_and_belongs_to_many :users, dependent: :destroy
has_many :posts, inverse_of: :group, dependent: :destroy
User Model:
has_many :posts, inverse_of: :user, dependent: :destroy
has_and_belongs_to_many :groups, dependent: :destroy
has_many :groupposts, through: :groups, source: :posts, class_name: "Post"
Post Model:
belongs_to :group, inverse_of: :posts
belongs_to :user, inverse_of: :posts
帖子的群组可能为空,因此用户可以在没有任何群组的情况下发布一般帖子 (group_id == nil),或者他们可以在他们所属的群组之一内发布帖子。
我试图弄清楚是否有一个事件记录查询可以给我:(1) 来自任何用户的所有一般帖子(无 group_id),加上 (2) 用户所属的任何群组的所有帖子。
所以类似:
Post.where(:group_id => nil)
独特地加入:
@user.groupposts
我认为它是在附近的东西:
Post.includes(:groups [:users]).where("(posts.group_id IS NULL) OR (posts.groups.users CONTAINS ?)", @user.id) # THIS IS NOT CORRECT, THE SECOND HALF OF THE 'OR' STATEMENT IS GARBAGE
最佳答案
您不能在同一个查询中得到该结果(如果您使用 UNION
编写完整的查询,则可以得到它),因为您的条件应用于同一字段,您应该有 2 个不同的结果查询它。
对于一般帖子,您可以尝试使用此方法
Post.where("group_id is null")
#or in scope
scope :general_posts, lambda { where ("group_id is null") }
来自用户组的所有帖子
Post.where( group_id: User.uniq.pluck(:id) )
#scope
scope :user_groups, lambda { where( group_id: User.uniq.pluck(:id) ) }
希望对您有帮助
关于mysql - 用于连接 has_and_belongs_to_many 关系的 ActiveRecord 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27934816/
我需要查找两个模型之间的记录。 class Report has_and_belongs_to_many :groups end class Group has_and_belongs_to_
我正在尝试在我的 Rails 应用程序中设置 Employee 模型和 NetworkDrive 模型之间的 has_and_belongs_to_many 关系。 在 employee.rb 中我指
我在用户和项目之间建立了多对多关系,而且它工作得很好。但是我想在 mongoid 中模拟这种情况: db.projects.insert( { "name" : "Test project
我有两个实体、数据中心和项目之间的多对多链接表,在遗留代码中。我发现,这实际上是一对多的关系。 作为清理关系的第一步,我想放一个唯一索引在其中一个字段上。 现在我收到以下错误: has_and_bel
我有一组非常简单的 HABTM 模型 class Tag tag) end end end 现在一切正常,除了标签表中出现大量重复项。 我需要做什么才能避免标签表中出现重复(基于名
我试图弄清楚 rails 是如何决定命名一个表的。我不得不反复试验才能弄清楚。 该表最终被命名为 menu_categories_items .我想知道它是如何做到的。 楷模 # == Schema
我对 HABTM 与 Rails 3.2.11 的关联有点困惑。 我有一个图像模型: class Image false do |t| t.references :image
我有研讨会表和类别表。 车间模型: has_many :workshop_categories, dependent: :destroy has_many :categories, throu
我有两个模型,Conversation 和 Phones,它们彼此有_and_belongs_to_many 个。电话可以有很多对话,对话可以有很多电话(两个或更多)。 class Conversat
修复者:更改模型名称以匹配 Rails 命名约定 当我尝试将技能加入用户时出现以下错误: irb(main):006:0> user.skills ' from script/rai
我试图让 mongoid 保存关联,但我只能让一侧工作。如果我有以下测试。 test "should add a user as a follower when a user follows th
我的 Ruby On Rails 项目中的 has_and_belongs_to_many 关联有问题。 这是我的模型: class Store false, :force => true do |
c = Course.create name:"Math1", ?? 我试图弄清楚如何将这门类(class)与 has_and_belongs_to_many 的现有学生联系起来。下面是我的关联模式。
预订 has_and_belongs_to_many学生们 学生 has_and_belongs_to_many图书 在 BooksStudents 模型中,我想在商店中添加“状态”字段,如果它是租用
嗨,我在模型中使用 has_and_belongs_to_many。 我想为种类设置存在的 valitor。 并将每个核心的最大种类数设置为 3 class Core 'core_id', :ass
一般混淆 我有可以有 3 种类型的乐队。我读了 previous SO post处理这个问题的正确方法是几个步骤: 1) 带内.rb has_and_belongs_to_many :genres 2
我有 field 和照片的模型: class Venue 这是我的 Edit 和 Update 方法的
我不知道这是否是 oracle_enhanced 适配器的问题。我有: class Room []) end end 日志 Started POST "/manuals" for 127.0
我正在尝试在 Ruby on Rails 项目中执行以下操作: class FoodItem :food_categories end class FoodCategory :food_categ
我有一个模型 UseCases(大约 6.000 行)和 EducationalObjectives(大约 4.000 行)与 has_and_belongs_to_many(EducationalO
我是一名优秀的程序员,十分优秀!