简体   繁体   中英

Ruby: merge results of two methods from a class

I've got a class called TemplateResources that has two methods like this:

def components
  @template.components
end

def custom_articles
  components += @user.custom_articles if @user.custom_articles.present?
end

The results are called in a view like so:

<% @template_resources.components.each do |article| %>

I want the combined results displayed in the view. I thought the += would do that, but it doesn't seem to. Anyone know how to join the results of those two methods?

Perhaps:

def components
  @template.components
end

def custom_articles
  @user.custom_articles || []
end

def all_articles
  components + custom_articles
end

And than in the view:

<% @template_resources.all_articles.each do |article| %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM