0

I'm getting the error "uninitialized constant Collection::CollectionComponent" from the check_box_tag at "@collection.components.include?" I'm not sure why this is happening as @collection seems to work fine in the form_for tag or if I remove the check_box_tag.

_form.html.haml

= form_for @collection do |f|
  - if @collection.errors.any?
    #error_explanation
      %h1= "#{pluralize(@collection.errors.count, "error")} prohibited this collection from being saved:"
      %ul
        - @collection.errors.full_messages.each do |msg|
          %li= msg

  .field
    - Component.all.each do |component|
      = label_tag :component_ids, component.id
      = check_box_tag :component_ids, component.id, @collection.components.include?(component), :name => 'collection[component_ids][]'
  .field
    = f.label :title
    = f.text_field :title
  .actions
    = f.submit 'Save'

collection.rb

class Collection < ActiveRecord::Base
  default_scope order('id ASC')

  attr_accessible         :style_id, 
                          :name, 
                          :title,
                          :component

  has_many                :collection_components, :dependent => :destroy
  has_many                :components, :through => :collection_components

  belongs_to              :style

  validates_presence_of   :style
  validates_presence_of   :title

  before_save             :create_name

  private

  def create_name
    self.name = title.parameterize
  end
end

component.rb

class Component < ActiveRecord::Base
  default_scope order('id ASC')

  attr_accessible         :category_id, 
                          :name, 
                          :title,
                          :collection,
                          :style

  has_many                :collection_components, :dependent => :destroy
  has_many                :collections,   :through => :collection_components

  has_many                :component_styles
  has_many                :styles,        :through => :component_styles

  belongs_to              :category

  validates_presence_of   :category
  validates_presence_of   :title

  before_save             :create_name

  private

  def create_name
    self.name = title.parameterize
  end
end

collection_components.rb

class CollectionComponents < ActiveRecord::Base
  attr_accessible :collection_id, 
                  :component_id

  belongs_to      :collection
  belongs_to      :component
end
Eric Norcross
  • 4,177
  • 4
  • 28
  • 53

1 Answers1

0

Rails is looking for a class with the name CollectionComponent but you only provide a class with the name CollectionComponents

Sascha Kaestle
  • 1,293
  • 12
  • 15
  • Why is it looking for "CollectionComponent" when I've used "CollectionComponents" in all references to the model? – Eric Norcross Apr 08 '13 at 08:36
  • This is one of the things you need to know about Rails: When you use plural form like ` has_many :component_styles` Rails assumes the name of this is singluar. Because its just more natural: "It has many component styles, which are made up of objects of the class ComponentStyle" – Sascha Kaestle Apr 08 '13 at 08:39
  • Ok that makes sense. I changed the model "CollectionComponents" to "CollectionComponent" and am now receiving a DB error: ActiveRecord::StatementInvalid - PG::Error: ERROR: column reference "id" is ambiguous LINE 1: ...on_id" IS NULL AND "components"."id" = 1 ORDER BY id ASC LIM... ^ – Eric Norcross Apr 08 '13 at 08:51
  • My guess is that this is happening because I generated the model incorrectly in the first place? – Eric Norcross Apr 08 '13 at 08:52
  • I don't understand your object design, so I can't help you out there.. It seems you should have a has_and_belongs_to_many association, maybe reading up on this will help? http://guides.rubyonrails.org/association_basics.html#the-has_and_belongs_to_many-association Actually the example they've used is exactly what you need in my opinion. – Sascha Kaestle Apr 08 '13 at 09:25
  • I'd appreciate you accepting the answer, by the way :) thanks – Sascha Kaestle Apr 08 '13 at 09:25
  • I'll have to toy around with it to see what the pg issue is. I chose a has_many :through relationship based off of input from colleagues as well as this post: http://blog.flatironschool.com/post/35346328762/why-you-dont-need-has-and-belongs-to-many – Eric Norcross Apr 08 '13 at 09:29