使用has_many:through访问属性

时间:2013-04-09 01:47:54

标签: ruby-on-rails associations has-many-through

在下面的代码中,我有两个通过has_many :through关联的模型。 Component模型也是belongs_to Category模型。我正在尝试通过Category.title Collection访问Component属性,例如:@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'

category.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

component.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

collection.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

1 个答案:

答案 0 :(得分:0)

如果您尝试编辑相关模型的属性,通常可以使用嵌套的fields_for作为关联模型。有关详细信息,请参阅Rails Form Helpers guidefields_for的文档。

但是,如果您主要只是对引用相关类别实例的title属性的正确方法感兴趣,那么这应该有效(用于访问第一个组件的类别) :

@collection.components.first.category.title

(请注意,我已将[0]替换为first,但它们实际上是相同的。)