通过在多对多关系中连接表属性来排序

时间:2013-03-19 02:10:32

标签: ruby-on-rails activerecord associations

我在简历和教育之间有多对多的关系,允许相同的教育条目出现在多个简历中。在简历上显示教育时,我希望为特定的简历订购教育。为此,我设置了一个连接表,Educations_Resumes,将订单信息作为属性。

然而,当我尝试一些像resume.educations时,我收到以下错误:

ActiveRecord::StatementInvalid: SQLite3::SQLException: near "order": syntax error: 
SELECT "educations".* FROM "educations" INNER JOIN "educations_resumes" ON 
"educations"."id" = "educations_resumes"."education_id" WHERE 
"educations_resumes"."resume_id" = 2 ORDER BY educations_resumes.order

模型设置为:

class Resume < ActiveRecord::Base
  has_many :educations_resumes
  has_many :educations, :through => :educations_resumes,
           :order => 'educations_resumes.order'

end

class EducationsResume < ActiveRecord::Base
  belongs_to :resume
  belongs_to :education
end

class Education < ActiveRecord::Base
  has_many :educations_resumes
  has_many :resumes, :through => :educations_resumes
end

有关如何正确订购resume.educations的任何建议都将非常感谢

1 个答案:

答案 0 :(得分:0)

您使用关键字作为列名:

http://www.sqlite.org/lang_keywords.html

运行迁移并更改列名:

rails g migration FixColumnName

这将为您提供一个空的迁移文件,您应该填写以下内容:

class FixColumnName < ActiveRecord::Migration
  def change
    rename_column :educations_resumes, :order, :ordering
  end
end