Estoy tratando de cargar una asociación anidada, un User puede pertenecer a muchos Groups y quiero cargar todas las Posts de los Groups de los que el usuario es miembro, pero solo las publicaciones, no los grupos también.
Algo así como current_user.groups.eager_load(:posts) , pero sin anidar publicaciones en grupos.
Supongo que Post pertenece al Group aquí.
Comience con el modelo que necesita cargar y cree la consulta a partir de ahí. Si el usuario pertenece a 2 grupos aquí, esto cargará todas las publicaciones de esos 2 grupos.
Post.where(group: current_user.groups)Puede configurar un acceso directo de User a Post con has_many: a través de
class User < ApplicationRecord has_and_belongs_to_many :groups has_many :posts, through: :groups # if User already has a `has_many :posts`, you'll need # to give the through-association a unique name like this: # has_many :group_posts, through: :groups, source: :posts end class Group < ApplicationRecord has_and_belongs_to_many :users has_many :posts end class Post < ApplicationRecord belongs_to :group # optionally, you can define the inverse here as well # has_many :users, through: :group end ... # eager_load on a query: User.includes(:posts) or User.eager_load(:posts) # get from current user: current_user.posts