comma-csv / comma

Comma is a small CSV (ie. comma separated values) generation extension for Ruby objects, that lets you seamlessly define a CSV output format via a small DSL

Home Page:https://github.com/comma-csv/comma

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Has Many Associations As Columns

JoshuaNovak919 opened this issue · comments

Is there a way to show a has many association as columns in an export? Basically i'm trying to have the user export have a column for each group with the header being the group name and the data being the date the group was created. Is there currently a way to do this?

Hi joshua,

I'd recommend making a custom method, delegate or use a lambda, or simply using the hash syntax.

eg. (given pages has a 'title')


class Book < ActiveRecord::Base
   has_many   :pages
   delegate :title, :to => :page

   comma do

     name
     description

     pages :title => 'This is a custom header for page title'
     title
     custom_method_title
   end

   def custom_method_title
     pages.find('x).title
   end
 end


cheers,

Tom