ruby on rails - Accessing properties with has_many :though -


in below code have 2 models associated via has_many :through. component model belongs_to category model. i'm trying access category.title property collection though component like: @collection.components[:id].category.title

in below code ":components" puts:

[#<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">] 

where each "#<component>" 1 checked form generated check boxes.

the problem can't figure out how access information. i've tried :components[0].category.title error:

undefined method `category' :components:symbol 

_form.html.haml

= form_for @collection |f|   - if @collection.errors.any?     #error_explanation       %h1= "#{pluralize(@collection.errors.count, "error")} prohibited collection being saved:"       %ul         - @collection.errors.full_messages.each |msg|           %li= msg    - category.products.each_slice(2) |column|     .column       - column.each |category|         .column-group           %h1              = category.title           - category.components.each |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 fill text_input [#<component...     / = f.text_field :components[0].category.title //this 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 

if you're trying edit property of related model, can typically use nested fields_for associated model. see documentation fields_for in rails form helpers guide details.

but if you're interested in correct way reference title attribute of associated category instance, should work (for accessing category of first component):

@collection.components.first.category.title 

(note i've replaced [0] first, they're same.)


Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -