thoughtbot/art_vandelay

How might we export calculated values?

Opened this issue · 1 comments

When exporting data, I want to be able to export calculated values, so that I have more context about each record.

Relates to #27

Here's a contrived example:

class User < ApplicationRecord
  has_many :posts

  def post_count
    post.count
  end
end

Another example is when using #find_by_sql.

We can return the calculated value there via SQL, but ArtVandelay::Export#csv checks if the records are ActiveRecord::Relation or ActiveRecord::Base. However, since #find_by_sql returns an Array, the Result is empty.

This seems like a promising approach since can can call #attributes on an instance of a record from a results returned from #find_by_sql, and that will include the calculated value,