1

以下のコードには、 を介して関連付けられた 2 つのモデルがありますhas_many :throughComponentモデルもbelongs_to Categoryモデル。私は次のようなものCategory.titleからプロパティにアクセスしようとしています:CollectionComponent@collection.components[:id].category.title

以下のコードでは、「:components」は次のように記述します。

[#<Component id: 23, name: "l-shaped-desks", title: "L-Shaped Desks", category_id: 10, created_at: "2013-04-07 00:18:07", updated_at: "2013-04-07 00:18:07">, #<Component id: 25, name: "writing-tables", title: "Writing Tables", category_id: 10, created_at: "2013-04-07 00:18:29", updated_at: "2013-04-07 00:18:29">]

それぞれ"#<Component>"が、生成されたチェック ボックスからチェックされるものです。

問題は、その情報にアクセスする方法がわからないことです。試してみまし:components[0].category.titleたが、エラーが発生しました:

undefined method `category' for :components:Symbol

_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

  - Category.products.each_slice(2) do |column|
    .column
      - column.each do |category|
        .column-group
          %h1 
            = category.title
          - category.components.each do |component|
            .checkbox
              = check_box_tag "component#{component.id}", component.id, @collection.components.include?(component), :name => 'collection[component_ids][]'
              = label_tag "component#{component.id}", component.title

  .field
    = f.label :title
    = f.text_field :title
  .field
    = f.label :components
    = f.text_field :components //This will fill a text_input with [#<Component...
    / = f.text_field :components[0].category.title //This does not work
  .actions
    = f.submit 'Save'

カテゴリ.rb

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

  CATEGORY_KINDS = [["Product", 0],["Page", 1]]

  scope :products, where(:kind => 0)
  scope :pages, where(:kind => 1)

  attr_accessible   :name, 
                    :title, 
                    :kind,
                    :section, 
                    :component

  has_many          :sections
  has_many          :components

  before_save       :create_name

  private

  def create_name
    self.name = title.parameterize
  end
end

コンポーネント.rb

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

  attr_accessible         :category_id, 
                          :name, 
                          :title,
                          :collection_ids,
                          :style_ids

  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

コレクション.rb

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

  attr_accessible               :style_id, 
                                :name, 
                                :title,
                                :component_ids


  has_many                      :collection_components, :include => :component
  has_many                      :components,            :through => :collection_components
  accepts_nested_attributes_for :collection_components, :allow_destroy => true

  belongs_to                    :style

  validates_presence_of         :style
  validates_presence_of         :title
  validates_presence_of         :component_ids

  before_save                   :create_name

  private

  def create_name
    self.name = title.parameterize
  end
end

collection_component.rb

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

  belongs_to      :collection
  belongs_to      :component
end
4

1 に答える 1