在Rails中创建has_and_belongs_to_many关系

时间:2016-01-07 00:44:21

标签: ruby-on-rails has-many

我有一个用户模型,它是在Michael Hartl RoR教程之后设计的,我正在尝试创建一个新的教师模型。我希望老师有很多用户,但每个用户只有一位老师。我用

创建了教师模型
class CreateTeachers < ActiveRecord::Migration
  def change
    create_table :teachers do |t|
      t.string :name
      t.string :email
      t.string :phone
      t.references :user, index: true, foreign_key: true

      t.timestamps null: false
    end
  end
end

并将has_one :teacher添加到user.rb.这是teachers.rb模型

class Teacher < ActiveRecord::Base
  has_and_belongs_to_many :users
  validates :user_id, presence: true
  before_save   :downcase_email
  validates :name, presence: true, 
                 length: { maximum: 50 }
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i
  validates :email, presence:   true, 
                    length: { maximum: 255 },
                    format:     { with: VALID_EMAIL_REGEX },
                    uniqueness: { case_sensitive: false }
  private

    # Converts email to all lower-case.
    def downcase_email
      self.email = email.downcase
    end
end

然而,在我的teacher_test.rb测试文件中,事情变得有些模糊。我试试这个

def setup
  @user = users(:michael)
  @user2 = users(:archer)
  @user3 = users(:lana)
  @user4 = users(:mallory)

  @teacher = Teacher.new(name: "Phred Willard",
                         email: "pwillard@test.com",
                         phone: "1234567890",
                         user_id: [@user.id, 
                                   @user2.id,
                                   @user3.id,
                                   @user4.id])
end

test "should be valid" do
  assert @uac.valid?
end

但是直接失败了。我是否正确地建立了自己的关系?我显然没有正确添加用户,因为模型未通过有效性测试。如何向该教师添加更多用户?提前谢谢。

2 个答案:

答案 0 :(得分:1)

你的老师.rb应该是

class Teacher < ActiveRecord::Base
  has_many :users
  before_save   :downcase_email
  validates :name, presence: true, 
             length: { maximum: 50 }
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i
  validates :email, presence:   true, 
                length: { maximum: 255 },
                format:     { with: VALID_EMAIL_REGEX },
                uniqueness: { case_sensitive: false }
  private

    # Converts email to all lower-case.
    def downcase_email
      self.email = email.downcase
    end
end

和user.rb

class User < ActiveRecord::Base
  belongs_to :teacher
  # rest of your code here ....
end

您需要在users表中使用teacher_id列。

答案 1 :(得分:1)

  

我希望老师有很多用户,但每个用户只有一位老师

您只需要has_many / belongs_to ...

#app/models/user.rb
class User < ActiveRecord::Base
   belongs_to :teacher
end

#app/models/teacher.rb
class Teacher < ActiveRecord::Base
   has_many :users
end

您需要在teacher_id表中添加users列(与您现有的相反):

class UpdateUsers < ActiveRecord::Migration
  def change
    change_table :users do |t|
      t.references :teacher, index: true, foreign_key: true #-> teacher_id
    end
  end
end

-

您遇到的错误是您在user_id上呼叫teacher;它应该是teacher_id用户:

@teacher = Teacher.new(name: "Phred Willard",
                     email: "pwillard@test.com",
                     phone: "1234567890",
                     user_ids: [@user.id, 
                               @user2.id,
                               @user3.id,
                               @user4.id])

这应该将@teacher与您列出的已定义@users相关联。

您还希望collection_singular_ids查看has_many关联,这就是您的测试失败的原因。