Using
result = Model.select(Model.table_name + '.*')
result = result.select(47 AS new_column)
result = result.where(...).order(...).limit(...) # etc.
result.to_a # gives an array of Model instances
I need to access the new_column value as an attribute of the Model instances.
I do not want to create a calculated attribute on the model; the data must come from the constructed query. The non-database column's data must get loaded as the Model instance is getting inflated by the #to_a
I have read Incorporating Custom SELECT clause in ActiveRecord query and Return custom columns in Rails ActiveRecord Query, which looked related but are not.
ActiveRecord query with nested models and derived column values?, Access SQL computed columns through ActiveRecord, and Adding a computed column to an ActiveRecord query look like they are answering my question, but they seem to say that the extra column values from the source query are automatically attached to the model instance, and one just adds accessors to get them (or not).
I have added attr_accessor
for the new columns' names to the model's class, but these attributes do not seem to be serialized when I return it as a graphQL response. Where can I find information about the actual mechanics of the AciveRecord#attributes array and how to add non-database columns to it ?
Cheers