在 Rails 中建立 has_and_belongs_to_many 关系
Creating a has_and_belongs_to_many relationship in Rails
我有一个根据 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
但这完全失败了。我是否正确建立了我的关系?我显然没有正确添加用户,因为该模型未通过有效性测试。我如何向该老师添加更多用户?提前致谢。
你的 teacher.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
用户 table 中需要 teacher_id 列。
I would like the teacher to have many users but each user to have only one teacher
你只需要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
您需要在 users
table 中添加一个 teacher_id
列(与您现在拥有的相反):
class UpdateUsers < ActiveRecord::Migration
def change
change_table :users do |t|
t.references :teacher, index: true, foreign_key: true #-> teacher_id
end
end
end
--
您遇到的错误是您在 teacher
上调用 user_id
;它应该是 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
关联,这就是您的测试失败的原因。
我有一个根据 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
但这完全失败了。我是否正确建立了我的关系?我显然没有正确添加用户,因为该模型未通过有效性测试。我如何向该老师添加更多用户?提前致谢。
你的 teacher.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
用户 table 中需要 teacher_id 列。
I would like the teacher to have many users but each user to have only one teacher
你只需要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
您需要在 users
table 中添加一个 teacher_id
列(与您现在拥有的相反):
class UpdateUsers < ActiveRecord::Migration
def change
change_table :users do |t|
t.references :teacher, index: true, foreign_key: true #-> teacher_id
end
end
end
--
您遇到的错误是您在 teacher
上调用 user_id
;它应该是 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
关联,这就是您的测试失败的原因。