danielfone / as_csv

Instant CSV support for Rails
MIT License
19 stars 9 forks source link

Relation .to_csv method isn't defined #8

Closed soynog closed 1 year ago

soynog commented 7 years ago

Not sure what I'm doing wrong... I've installed the gem. ActiveModel::Serialization is included by models. Individual model to_csv method works fine. But to_csv on a relation doesn't work:

irb(main):014:0> Service < ActiveModel::Serialization
=> true
irb(main):015:0> Service.first.to_csv
=> "id,created_at,updated_at,type,name,gtfs_agency_id,logo,email,url,phone,start_or_end_area_id,trip_within_area_id,fare_structure,fare_details,archived\n1,2017-04-05 14:01:12 -0400,2017-04-20 13:34:28 -0400,Transit,Sample Transit Service,\"\",,,http://www.mbta.com,555-555-5555,,,,{:fare_table=>{}},false\n"
irb(main):016:0> Service.all.to_csv
undefined method `to_csv' for #<Service::ActiveRecord_Relation:0x007f9a4bbf5b20>
Did you mean?  to_s

From the readme it sounds like I should be able to just install the gem, include Serialization on my model, and call to_csv on a relationship. Am I missing something?

Thanks!

FYI I'm using Ruby 2.4.0, Rails 5.0.2

danielfone commented 7 years ago

Hi @soynog, what version of Rails are you using?

Edit. Just saw your comment at the end. I haven't checked if this gem works with Rails 5 yet, which it obviously doesn't. If I get some time, I'll investigate a Rails 5 compatibility fix.

soynog commented 7 years ago

Sounds good--probably that's the issue. Thanks!

senych-vitalii commented 7 years ago

I have the same issue in Rails 5.0.2 but without using active model serialization.

danielfone commented 7 years ago

@kaleb4eg @soynog this gem adds to_csv on two classes: Array and ActiveModel::Serialization. In previous versions of Rails, this was the pattern that was used for the other serializers. You can still get the benefit of the gem by either casting your collection to an Array, or using AsCSV::CSVBuilder manually.

danielfone commented 1 year ago

Closing this issue as out of date. Latest versions should work fine.