How do I convert the following to a clear one-liner in ruby?
def questions
results = []
sections.each do |section|
results = results.concat(Question.find_all_by_survey_section_id(section.id))
end
results
end
I feel like I could use the &: (Symbol#to_proc) and returning methods in there somehow.
Assuming that SurveySection
has_many :questions, you can simplify this to:Or if the method is defined in an ActiveRecord model, you could just declare that it
has_many :questions, :through => :sectionsand you won’t have to define the method at all.