0

guys! I'm trying to make right architecture decision:

I need to hide from user of my site some fields of other users by default. So not to bother with filtering the fields in views I want to not load those fields at all, like:

default_scope -> { select(column_names - FILTERED_PARAMS) }

the rest of fields should be loaded explicitly in special cases.

The problem is that once code refers to the missing fields nomethod error shows up. I tried to meta-program those fields but fruitless this far. It seems to me that this approach doesn't fit to the AR object life-cycle.

Have you ever implemented such functionality if so what pattern have you chosen?

Roaring Stones
  • 1,054
  • 7
  • 22

1 Answers1

0

From my experience the best decision would be not to filter these params on the query with select, but to filter what parameters are actually sent to the user. my_model.as_json (with given param filtering options) is a simple solution for that, but for more advanced uses I would advise Rabl gem https://github.com/nesquena/rabl That way you have more control over which params are returned even in very advanced cases in a model-view-controller manner.

Vitaly Stanchits
  • 658
  • 2
  • 7
  • 24
  • Thank you! The decision was dictated by the quite complex authentication policy and variant field access for every role along with varied output: aside html, the rails server also serves JSON endpoints. So I wanted to not mess with view-level filter and tried to get easy solution. – Roaring Stones Nov 26 '14 at 14:21
  • If so, then generally about select, I don't think it takes an array as input variable. You would need something like -> { select(*(allowed_params - notallowed_params)) } – Vitaly Stanchits Nov 26 '14 at 17:27