Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 293361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T06:15:53+00:00 2026-05-12T06:15:53+00:00

Been trying to sort this for over a day now, and I am sure

  • 0

Been trying to sort this for over a day now, and I am sure that it is something simple that I am missing.

I have a project, which can have one main category and two optional categories. My relevant code for the project model:

has_many :project_categories

has_one  :optional_category_1,
         :through => :project_categories,
         :conditions => 'is_main_category = 0',
         :order => 'category_id',
         :source => :category,
         :class_name => 'Category'

has_one  :optional_category_2,
         :through => :project_categories,
         :conditions => 'is_main_category = 0',
         :order => 'category_id DESC',
         :source => :category,
         :class_name => 'Category'

has_one  :main_category,
         :through => :project_categories,
         :conditions => 'is_main_category = 1',
         :source => :category,
         :class_name => 'Category'

The relevant code from the Category class:

has_many :project_categories
has_many :projects, :through => :project_categories, :source => :project

and from the ProjectCategory class:

class ProjectCategory < ActiveRecord::Base
  belongs_to :project
  belongs_to :category
end

In my view:

    Main Category: <%= f.select(:main_category, Category.find(:all, :order => 'parent_id, categories.desc').collect {|c| [c.display_name, c.id] }, :prompt => "Select a Main Category") %><br>
Optional Category 1: <%= f.select(:optional_category_1, Category.find(:all, :order => 'parent_id, categories.desc').collect {|c| [c.display_name, c.id] }, :prompt => "Select an Optional Category") %><br>
Optional Category 2: <%= f.select(:optional_category_2, Category.find(:all, :order => 'parent_id, categories.desc').collect {|c| [c.display_name, c.id] }, :prompt => "Select an Optional Category") %><br>

and in my controller:

      @project.attributes = params[:project]

Ok, so when updating an existing project, I get the following error:

undefined method `update_attributes' for #<Class:0x82efce0>

and the relevant stack trace:

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'

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.

Any help appreciated!

Vikram

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T06:15:53+00:00Added an answer on May 12, 2026 at 6:15 am

    I know this doesn’t address the error you’re getting, but I’d suggest using three one-to-many relationships instead of one many-to-many relationship.

    The conventional purpose of has_many :through => ... (many-to-many) is for when you have something like students and classes. A student can be in any number of classes. A class can have any number of students. Totally arbitrary numbers on both sides of the relationship.

    But that isn’t your situation here. Your projects can be in exactly one main category, one optional category 1, and one optional category 2. It’s a totally different problem and it isn’t the problem that has_many :through is designed to solve.

    I suggest this arrangement:

    class Project < ActiveRecord::Base
    
      belongs_to :main_category, :class_name => "Category",
        :foreign_key => 'main_category_id'
    
      belongs_to :optional_category_1, :class_name => "Category",
        :foreign_key => 'optional_category_1_id'
    
      belongs_to :optional_category_2, :class_name => "Category",
        :foreign_key => 'optional_category_2_id'
    
    end
    
    class Category < ActiveRecord::Base
    
      has_many :main_category_projects, :class_name => "Project",
        :foreign_key => 'main_category_id'
    
      has_many :optional_category_1_projects, :class_name => "Project",
        :foreign_key => 'optional_category_1_id'
    
      has_many :optional_category_2_projects, :class_name => "Project",
        :foreign_key => 'optional_category_2_id'
    
    end
    

    Then you’ll be able to do stuff like:

    my_project.main_category
    
    my_category.optional_category_1_projects
    
    # etc...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have been over so many posts online trying to sort this.... i am
Just need help as I have been trying sort this out for ages now.
I have been trying to sort this problem out for days now and I
I've been trying to sort this out for a while now, I have checked
Been trying to find this online for a while now. I have a SDL_Surface
I have been trying to sort this problem out for the last few days.
I've been trying for days now to try and sort this out. Today was
Been trying to get this up and running for a while now. Basically i
I have been trying to figure this out until my brain imploded. Im trying
i have been trying to sort alias field defined in the setselect method as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.