I’m trying to pull ou the ‘name’ field for each of my associated rows
and pass it as a string of names. I’m missing something obvious here
but I thought @bag.packages.name.join(", ") would work.
do I have to loop through @bag.packages? that seems like the long way.
I’m trying to pull ou the ‘name’ field for each of my associated rows
and pass it as a string of names. I’m missing something obvious here
but I thought @bag.packages.name.join(", ") would work.
do I have to loop through @bag.packages? that seems like the long way.
You have to loop or do a custom query. Use the map message to do the
loop:
@bag.packages.map { |p| p.name }.join(", ")
In Edge/1.1 you can type that as @bag.packages.map(&:name).join(", ")
If you don’t want to fetch all the package data but only get the list of
names, you’ll have to construct a custom query using the :select option
on find(). This is probably a good use for an association extension.
I’m trying to pull ou the ‘name’ field for each of my associated rows
and pass it as a string of names. I’m missing something obvious here
but I thought @bag.packages.name.join(", ") would work.
do I have to loop through @bag.packages? that seems like the long way.
You can’t ask the packages collection for the names of all the packages
it contains. You do have to iterate over the collection, but it doesn’t
involve a lot of code.
try
@bag.packages.collect {|p| p.name}.join(’, ')
regards
Justin
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.