ruby on rails - Prevent has_many lookup during json serialization -


i have query this

@templates = template.find_by_sql("select templates.*, jsonb_agg(fields.*) fields templates left join fields on templates.id = fields.template_id group templates.id ") 

inside model defined has_many relationship

has_many :fields, dependent: :destroy 

inside controller render result with

render json: @templates 

the resulting json formatted expected rails additional unnecessary queries:

template load (0.6ms)  select templates.*, jsonb_agg(fields.*) fields templates left join fields on templates.id = fields.template_id group templates.id  rails_1  |   field load (0.3ms)  select "fields".* "fields" "fields"."template_id" = $1  [["template_id", 1]] rails_1  |   field load (0.4ms)  select "fields".* "fields" "fields"."template_id" = $1  [["template_id", 2]] rails_1  |   field load (0.3ms)  select "fields".* "fields" "fields"."template_id" = $1  [["template_id", 3]] 

when change column name inside query example:

@templates = template.find_by_sql("select templates.*, jsonb_agg(fields.*) fields_sql templates left join fields on templates.id = fields.template_id group templates.id ") 

no additional queries performed end , unwanted key in json (fields_sql instead of fields)


Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -