activerecord - How to get ActiveRecord_Relation based on a ActiveRecord_Relation in rails? -


i have models user course

user.rb

has_many :courses 

course.rb

belongs_to :user 

if have activerecord_relation @users, list of user.

how activerecord_relation @courses, list of course based on @users

i know can done @users.each |user| 1 one. there easier way it? @users.courses?

you can use #flat_map method ruby:

@users.flat_map(&:courses) 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -