在下面的代码中,我有两个模型通过has_many :through
. Component
模型也是belongs_to Category
模型。我正在尝试从 a 访问该属性Category.title
,例如:Collection
Component
@collection.components[:id].category.title
在下面的代码“:组件”中放置:
[#<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
集合组件.rb
class CollectionComponent < ActiveRecord::Base
attr_accessible :collection_id,
:component_id
belongs_to :collection
belongs_to :component
end