Jump to content

The ultimate community for Ruby on Rails developers.


fields_for using custom query

fields_for views nested_attributes

  • Please log in to reply
No replies to this topic

#1 larabosa



  • Members
  • 13 posts

Posted 21 August 2013 - 04:05 PM



I am wondering if it is possible to create a custom sql to be used within a fields_for and when editing avoid duplicates or if I should build my custom query and loop through the results in a different way.


The reason I am asking is because I have nested attributes that only need a text field named "suggested amount" and a text area named "comments" as many times as resources I have, but within the fields_for I need to display the resource name for each resource_id and another resource amount coming from a different table. I was thinking of something like this:

<fields_for :reviewer_resources >

resource_name, pi_requested_amount, text_field for suggested_amount, text_area for comments


when saving for the first time it will create the suggested_amount for each resource_id, but I am wondering how to approach the Edit part since fields_for will bring the existing children and I also need to build suggested_amount and comments for the other resources that were not chosen. If my custom query includes the suggested_amount and comments too,  do I need fields_for at all?. 


Is this doable or should I approach it in a different way?.


I've been struggling with this for weeks now so any help is much appreciated.

Edited by Jamie, 21 August 2013 - 05:02 PM.
Code tags added

Also tagged with one or more of these keywords: fields_for, views, nested_attributes

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users