I am creating a custom form builder.
A Form has_many Sections .
A Section has_many Fields .
A Field has_many User_Values .
There is a column on the User_Values table called cloned_index . I use this for when a user makes a duplicate of a section but need to keep the sections unique.
My Question:
Can someone help me write a query that for every section, group the fields by the cloned_index of the field's user values.
I Currently Have:
form.sections.each do |section| section.fields.each do|field|
field.user_values.group_by(&:cloned_index)
end
This currently doesn't work and is very tough to follow. I'm sure there's an easy way to do this that I'm just not seeing right now.
form.sections.joins(fields: [:user_values]).group("user_values.cloned_index")
You need to join
the tables to group them together. The above query will return the sections
grouped by cloned_index
If you want to group the fields
along with sections
then try
fields.joins(:user_values,:section]).where({sections: {form_id: form.id}}).group("sections.id , user_values.cloned_index")
This will return fields of the form grouped by section ID
and cloned_index
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.