Avir94 Avir94 - 10 months ago 39
Ruby Question

using options_for_select in nested form rails @class.associative_class?

In my rails application, Im using a form for costs and a nested form for dependent_costs.

Costs has_many dependent_costs and dependent_costs belong_to costs.

This is where I am having issues:

<div class="col-xs-12" id='dependency'>
<section id="new-dependency">
<div class="col-xs-3 center-block">
<%= f.fields_for :cost_dependencies, @cost.cost_dependency do |dependency| %>
<%= dependency.label :dependency_category, "Dependent Category" %>
<%= dependency.select :dependency_category, options_for_select(getAllCategories, :selected => @cost.cost_dependency.dependency_category.nil? ? 'Please Select' : @cost.cost_dependency.dependency_category), {}, {:class => 'form-control', id: "category-select"} %>

It's at the
part. I receive a no method dependency_category for
#<ActiveRecord::Associations::CollectionProxy []>

and i'm not quite sure where to move forward from here. because I would like to use this form for both new and edit methods as I have done in the past.

Misc files in case that is helpful:
Model cost and cost_dependency:

class Cost < ApplicationRecord
has_many :cost_dependency
validates :per_job, numericality: true
validates :per_page, numericality: true
accepts_nested_attributes_for :cost_dependency


class CostDependency < ApplicationRecord
belongs_to :cost
validates :per_job, numericality: true
validates :per_page, numericality: true

Any help would be appreciated.

Thank you.

Answer Source

returns a collection, ActiveRecord::Associations::CollectionProxy, not an not an instance of the CostDependency class, thus error.

May be you've meant to write the following?


I assume, that dependency_category is an attribute of CostDependency model.