prevent certain column values from returning when calling an active record instance (without limiting access to them in future)

StackOverflow https://stackoverflow.com/questions/19389562

Question

I have a database which is postgis enabled which has some records (in a given table, there are many) that have enormous :spatial column values.

I often use the awesome_print gem to view records quickly while working. It colorizes and nicely displays a given record (or records') information for quick review. The problem in this case is that 99% of the terminal display is devoting to showing these spatial column's multi-page length list of coordinates in the WKT format.

I'd like for activerecord to not return these objects when viewing these with the ap (awesome print) command. Is there any way to do that without breaking something else? Can I just instruct ActiveRecord to hide these column's values unless specifically requested or is that too much to ask?

Was it helpful?

Solution

One way or another you need to designate which fields to print, or not to print. For instance you could define a helper for this and put it, say, in the your console config file, e.g.:

def ap_article(article, cols=%w[col1 col2 col3])
  ap article.attributes.slice(*cols)
end

or perhaps something like this if you just want to ignore the spatial columns

def ap_article(article)
  cols = article.class.columns.select {|c| c.type != :spatial}.map(&:name)
  ap article.attributes.slice(*cols)
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top