Jump to content

The ultimate community for Ruby on Rails developers.


How to build a subform with data from different models and avoid duplicates with fields_for

  • Please log in to reply
No replies to this topic

#1 larabosa



  • Members
  • 13 posts

Posted 19 August 2013 - 04:06 PM

I'm working on a Rails project where I need to provide the user with a subform as follows: For each of N resources, a text string with the resource name (resource model), text string with an amount a user entered (action_resource model) and a text box where a reviewer user can enter a desired amount. Upon submission of the form, my code saves -only- those M <= N resource-amount pairs for which the user has entered something.
I want the user to be able to go back to this form at any time. The form would retrieve the M resource-amount pairs and display all N resources as before (obviously the N-M resources not specified would have an empty text box).
The models look like this:
class RequestReview < ActiveRecord::Base

    attr_accessible :request_id, :panel_member_id, :request_status_type_id, :overall_rating_type_id, :review,

    validates(:review, presence: true)
    validates(:overall_rating_type_id, presence: true)

    before_save { self.date_reviewed = Time.now}

    self.primary_key = :request_review_id

    belongs_to :panel_member, :class_name => 'PanelMember', :foreign_key => :panel_member_id    
    belongs_to :request, :class_name => 'Request', :foreign_key => :request_id    
    belongs_to :overall_rating_type, :class_name => 'OverallRatingType', :foreign_key => :overall_rating_type_id    
    has_many :request_reviewer_resources, :class_name => 'RequestReviewerResource', :foreign_key => :request_review_id,
             :dependent => :destroy

    # Reject children if no suggested amount entered by reviewer
    accepts_nested_attributes_for :request_reviewer_resources, :allow_destroy => true,
                                  :reject_if => proc { |att| att['suggested_amount'].blank? }
class RequestReviewerResource < ActiveRecord::Base

    attr_accessible :resource_id, :suggested_amount, :comments

    self.primary_key = :request_reviewer_resource_id

    belongs_to :resource, :class_name => 'Resource', :foreign_key => :resource_id
    belongs_to :request_review, :class_name => 'RequestReview', :foreign_key => :request_review_id
class ActionResource < ActiveRecord::Base

    self.primary_key = :action_resource_id

    belongs_to :resource_amount_type, :class_name => 'ResourceAmountType', :foreign_key => :resource_amount_type_id    
    belongs_to :resource, :class_name => 'Resource', :foreign_key => :resource_id    
    belongs_to :action, :class_name => 'Action', :foreign_key => :action_id    
class Action < ActiveRecord::Base

    self.primary_key = :action_id

    belongs_to :alloc_date_type, :class_name => 'AllocDateType', :foreign_key => :alloc_date_type_id    
    belongs_to :action_type, :class_name => 'ActionType', :foreign_key => :action_type_id    
    belongs_to :request, :class_name => 'Request', :foreign_key => :request_id    
    has_many :action_documents, :class_name => 'ActionDocument', :foreign_key => :action_id    
    has_many :action_resources, :class_name => 'ActionResource', :foreign_key => :action_id    
Any help would be appreciated

  • Jemagee likes this

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users