Load model partly in Ruby On rails

98 Views Asked by At

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?

1

There are 1 best solutions below

2
On

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.