Note that there are some explanatory texts on larger screens.

plurals
  1. POhas_many through update issue
    primarykey
    data
    text
    <p>Been trying to sort this for over a day now, and I am sure that it is something simple that I am missing.</p> <p>I have a project, which can have one main category and two optional categories. My relevant code for the project model:</p> <pre><code>has_many :project_categories has_one :optional_category_1, :through =&gt; :project_categories, :conditions =&gt; 'is_main_category = 0', :order =&gt; 'category_id', :source =&gt; :category, :class_name =&gt; 'Category' has_one :optional_category_2, :through =&gt; :project_categories, :conditions =&gt; 'is_main_category = 0', :order =&gt; 'category_id DESC', :source =&gt; :category, :class_name =&gt; 'Category' has_one :main_category, :through =&gt; :project_categories, :conditions =&gt; 'is_main_category = 1', :source =&gt; :category, :class_name =&gt; 'Category' </code></pre> <p>The relevant code from the Category class:</p> <pre><code>has_many :project_categories has_many :projects, :through =&gt; :project_categories, :source =&gt; :project </code></pre> <p>and from the ProjectCategory class:</p> <pre><code>class ProjectCategory &lt; ActiveRecord::Base belongs_to :project belongs_to :category end </code></pre> <p>In my view:</p> <pre><code> Main Category: &lt;%= f.select(:main_category, Category.find(:all, :order =&gt; 'parent_id, categories.desc').collect {|c| [c.display_name, c.id] }, :prompt =&gt; "Select a Main Category") %&gt;&lt;br&gt; Optional Category 1: &lt;%= f.select(:optional_category_1, Category.find(:all, :order =&gt; 'parent_id, categories.desc').collect {|c| [c.display_name, c.id] }, :prompt =&gt; "Select an Optional Category") %&gt;&lt;br&gt; Optional Category 2: &lt;%= f.select(:optional_category_2, Category.find(:all, :order =&gt; 'parent_id, categories.desc').collect {|c| [c.display_name, c.id] }, :prompt =&gt; "Select an Optional Category") %&gt;&lt;br&gt; </code></pre> <p>and in my controller:</p> <pre><code> @project.attributes = params[:project] </code></pre> <p>Ok, so when updating an existing project, I get the following error:</p> <pre><code>undefined method `update_attributes' for #&lt;Class:0x82efce0&gt; </code></pre> <p>and the relevant stack trace:</p> <pre><code>C:/Software/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/associations.rb:1255:in `main_category=' C:/Software/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/base.rb:2745:in `send' C:/Software/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/base.rb:2745:in `attributes=' C:/Software/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/base.rb:2741:in `each' C:/Software/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/base.rb:2741:in `attributes=' C:/Development/craftbits_rails/app/controllers/projects_controller.rb:85:in `manage_project' </code></pre> <p>Is it saying that there is an issue with main_category and that it is a generic class? But why? The association defines it correctly AFAIK.</p> <p>Any help appreciated!</p> <p>Vikram</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload