简体   繁体   中英

rails - ruby - get list items in another list

I have a list of object, contaning

obj1
  status
  name
  date
  etc...
obj2
  status
  name
  date
  etc...
etc.

And I want to create an another list contaning only names of every objects.

For now, I use this

  @object.each do |obj|
    @users << obj.user.name
  end

It's working, but I thinks there is a better and faster way to do this.

Anyone ?

You can use

@user_names = @object.map { |obj| obj.user.name }

which returns an array.

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