现在试图对它进行一天的排序,我确信这是我想念的简单事项。
我有一个项目,可以有一个主要类别和两个可选类别。我的项目模型的相关代码:
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'
Category类的相关代码:
has_many :project_categories
has_many :projects, :through => :project_categories, :source => :project
并从ProjectCategory类:
class ProjectCategory < ActiveRecord::Base
belongs_to :project
belongs_to :category
end
在我看来:
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>
并在我的控制器中:
@project.attributes = params[:project]
好的,所以在更新现有项目时,我收到以下错误:
undefined method `update_attributes' for #<Class:0x82efce0>
和相关的堆栈跟踪:
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'
是否说main_category存在问题并且它是泛型类?但为什么?该协会正确定义了AFAIK。
任何帮助表示赞赏!
维克拉姆
答案 0 :(得分:0)
可能会将控制器更改为:
@project = Project.find(params[:id])
@project.update_attributes(params[:project])
答案 1 :(得分:0)
您可能需要在视图中使用main_category_id
,即
Main Category: <%= f.select(:main_category_id, ...) %>
您正在呼叫update_attributes
,但main_category
不是正确的属性 - 它是一种关联。 main_category_id
是一个属性。
答案 2 :(得分:0)
你从哪里获得@project
?你只是做一个正常的Project.find(params[:project_id])
或什么?
尝试投入debugger
语句,看看@project是什么类,以及它有哪些方法?
答案 3 :(得分:0)
我知道这并没有解决你所得到的错误,但我建议使用三个一对多关系,而不是一对多关系。
has_many :through => ...
(多对多)的常规用途适用于students
和classes
之类的内容。学生可以在任何课程中。一个班级可以有任何数量的学生。关系两边完全是任意数字。
但这不是你的情况。您的项目可以只有一个主要类别,一个可选类别1和一个可选类别2.这是一个完全不同的问题,并不是has_many :through
旨在解决的问题。
我建议这样安排:
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
然后你就能做到这样的事情:
my_project.main_category
my_category.optional_category_1_projects
# etc...