у меня есть User
модель
class User < ApplicationRecord
has_many :posts, dependent: :destroy
has_many :collections, dependent: :destroy
# here, I want to get :collected_posts (all posts that all user collections have)
# has_many :collected_posts, through: :collections, source: :post (this throws error)
end
Это моя Post
модель
class Post < ApplicationRecord
belongs_to :user
has_many :post_collections, dependent: :destroy
end
Это моя Collection
модель
class Collection < ApplicationRecord
belongs_to :user
has_many :post_collections, dependent: :destroy
has_many :posts, through: :post_collections
end
Это моя PostCollection
модель
class PostCollection < ApplicationRecord
belongs_to :post
belongs_to :collection
end
Я хочу сделать current_user.collected_posts
, чтобы получить все записи, которые он сохранил во всех своих коллекциях.
Но я получаю эту ошибку
# ActiveRecord::HasManyThroughSourceAssociationNotFoundError (Could not find the source association(s) :post in model Collection. Try 'has_many :collected_posts, :through => :collections, :source => <name>'. Is it one of user, post_collections, or posts?)
потому что в объекте коллекции нет post_id
.
Как я могу получить все сообщения, которые есть во всех пользовательских коллекциях?
Спасибо!