On Tue, Sep 9, 2008 at 10:11 PM, John Small <
[EMAIL PROTECTED]> wrote:

>
> I have a model, Account, with many Courses. I want to display the course
> codes for those courses as comma delimited text in an attribute of the
> account. So I would have
>
> Account << ActiveRecord::Base
> has_many :courses
>
> def get_course_codes_as_text
>  courses.each {|course| something something but I don't know what
> end
>
> Now I could do it the hard way by setting up a string var and pumping
> stuff into it inside the block. But there's an easier Rubyesque way to
> do this which I've seen in code elsewhere but I can't find the code now
> so I thought I'd ask here. Something to do with join or map or something
>
> John Small
> --
> Posted via http://www.ruby-forum.com/.
>
> >
>
a = []
courses.each { |course|
a.push(course.code)
}
a = a.join ","


This ought to do it.


~~
Shiv

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to