【问题标题】:Has_one association not workingHas_one 关联不起作用
【发布时间】:2012-11-06 00:17:23
【问题描述】:

我不知道这个关系的错误在哪里:

class Education < ActiveRecord::Base
  attr_accessible :description, :graduation, :student_id, :faculty_id, :department_id

  has_one :faculty
  belongs_to :student
end
#  id          :integer          not null, primary key
#  student_id  :integer
#  faculty_id  :integer
#  description :text
#  graduation  :string(255)

class Faculty < ActiveRecord::Base
  attr_accessible :department_id, :name

  belongs_to :education
  belongs_to :department
end

# == Schema Information
#
# Table name: faculties
#
#  id            :integer          not null, primary key
#  name          :string(255)
#  department_id :integer

为什么我必须将education_id 添加到faculties 表中? 每个教育只有一个教员,教员属于多个教育。

>> Education.last.faculty
  Education Load (0.3ms)  SELECT "educations".* FROM "educations" ORDER BY "educations"."id" DESC LIMIT 1
  Faculty Load (0.2ms)  SELECT "faculties".* FROM "faculties" WHERE "faculties"."education_id" = 1 LIMIT 1
SQLite3::SQLException: no such column: faculties.education_id: SELECT  "faculties".* FROM "faculties"  WHERE "faculties"."education_id" = 1 LIMIT 1
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: faculties.education_id: SELECT  "faculties".* FROM "faculties"  WHERE "faculties"."education_id" = 1 LIMIT 1
from /Users/rege/.rvm/gems/ruby-1.9.3-p194@ckdatabase/gems/sqlite3-1.3.6/lib/sqlite3/database.rb:91:in `initialize'

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3.1 associations


    【解决方案1】:

    无论你在哪一边放置一个 'belongs_to' 都会有一个相关对象的 id - 它是根据定义来的。如果你属于某个东西,你就不属于另一个实例。

    如果您希望每个教育只有一个教员,有两种选择:

    • 使用一对一:当您还希望教师只有一种教育时
    • 将 belongs_to 与一对多结合使用:当您希望每个教员拥有多个教育时

    所以你可以试试这个:

    class Education < ActiveRecord::Base
      attr_accessible :description, :graduation, :student_id, :faculty_id, :department_id
    
      belongs_to :faculty
      belongs_to :student
    end
    
    class Faculty < ActiveRecord::Base
      attr_accessible :department_id, :name
    
      has_many :education
      belongs_to :department
    end
    

    【讨论】:

      【解决方案2】:

      我认为你们的关系有些混乱 - official docs 可能会有所帮助。

      如果我理解你想要做什么,你需要这样的东西(关于你的教师/教育关系):

      class Education < ActiveRecord::Base
        attr_accessible :description, :graduation, :student_id, :faculty_id, :department_id
      
        belongs_to :faculty # foreign key - faculty_id
        belongs_to :student
      end
      #  id          :integer          not null, primary key
      #  student_id  :integer
      #  faculty_id  :integer
      #  description :text
      #  graduation  :string(255)
      
      class Faculty < ActiveRecord::Base
        attr_accessible :department_id, :name
      
        has_many :educations
        belongs_to :department
      end
      

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多