从多个主题中获取所有 post
Getting all the post from multiple subjects
我想显示所有 post 只与用户的所有主题相关的内容。
我试了又试,但总是出错。这太混乱了,因为它对我来说有很多关系。这是模型。
class Post
include Mongoid::Document
include Mongoid::Timestamps
field :content, type: String
field :locked , type: Boolean
field :unlocked_at , type: Date
validates_presence_of :content
belongs_to :subject
belongs_to :user , inverse_of: :posted_by , class_name: "User"
has_many :comments,dependent: :delete
has_many :video_attachments , inverse_of: :posts , dependent: :delete
accepts_nested_attributes_for :video_attachments, autosave: true
accepts_nested_attributes_for :comments, autosave: true
end
require "mongoid/token"
class Subject
include Mongoid::Document
include Mongoid::Token
field :title , type: String
field :desc , type: String
has_and_belongs_to_many :admins , inverse_of: :admin_of , class_name: "User"
has_and_belongs_to_many :students , inverse_of: :student_of , class_name: "User"
has_many :posts
token length: 6
end
class User
include Mongoid::Document
authenticates_with_sorcery!
mount_uploader :avatar , AvatarUploader
#fields
#field :email , type: String
field :id_number , type: String
field :crypted_password , type: String
field :salt , type: String
field :fname , type: String
field :lname , type: String
field :mname , type: String
field :avatar
field :gender , type: Boolean
field :role , type: String , default: "Student"
#relations
has_and_belongs_to_many :admin_of , inverse_of: :admins , class_name: "Subject"
has_and_belongs_to_many :student_of , inverse_of: :students , class_name: "Subject"
has_one :user_detail
has_many :posts , inverse_of: :user , class_name: "Post"
has_many :comments
accepts_nested_attributes_for :user_detail , autosave: true
end
current_user = User.find("5492ca884d696c7bbe040000")
posts = current_user.student_of.collect(&:posts).flatten
我想显示所有 post 只与用户的所有主题相关的内容。 我试了又试,但总是出错。这太混乱了,因为它对我来说有很多关系。这是模型。
class Post
include Mongoid::Document
include Mongoid::Timestamps
field :content, type: String
field :locked , type: Boolean
field :unlocked_at , type: Date
validates_presence_of :content
belongs_to :subject
belongs_to :user , inverse_of: :posted_by , class_name: "User"
has_many :comments,dependent: :delete
has_many :video_attachments , inverse_of: :posts , dependent: :delete
accepts_nested_attributes_for :video_attachments, autosave: true
accepts_nested_attributes_for :comments, autosave: true
end
require "mongoid/token"
class Subject
include Mongoid::Document
include Mongoid::Token
field :title , type: String
field :desc , type: String
has_and_belongs_to_many :admins , inverse_of: :admin_of , class_name: "User"
has_and_belongs_to_many :students , inverse_of: :student_of , class_name: "User"
has_many :posts
token length: 6
end
class User
include Mongoid::Document
authenticates_with_sorcery!
mount_uploader :avatar , AvatarUploader
#fields
#field :email , type: String
field :id_number , type: String
field :crypted_password , type: String
field :salt , type: String
field :fname , type: String
field :lname , type: String
field :mname , type: String
field :avatar
field :gender , type: Boolean
field :role , type: String , default: "Student"
#relations
has_and_belongs_to_many :admin_of , inverse_of: :admins , class_name: "Subject"
has_and_belongs_to_many :student_of , inverse_of: :students , class_name: "Subject"
has_one :user_detail
has_many :posts , inverse_of: :user , class_name: "Post"
has_many :comments
accepts_nested_attributes_for :user_detail , autosave: true
end
current_user = User.find("5492ca884d696c7bbe040000")
posts = current_user.student_of.collect(&:posts).flatten