我创建了通过 has_many/belongs_to 关系关联的 Trainer 和 Client 模型:Trainer has_many Clients 和 Client belongs_to Trainer。但是,当尝试使用 将现有客户端添加到现有培训师Trainer.Clients << Client
时,它不会保存到数据库中。
客户型号:
class Client < ActiveRecord::Base
attr_accessible :email, :name, :password, :password_confirmation, :trainer_id
has_secure_password
belongs_to :trainer
validates :name, presence: true, length: { maximum: 40 }
validates :password, presence: true, length: { minimum: 6 }
validates :password_confirmation, presence: true, length: { minimum: 6 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
before_save { |client| client.email = email.downcase }
end
教练机型号:
class Trainer < ActiveRecord::Base
attr_accessible :email, :name, :password, :password_confirmation
has_secure_password
has_many :clients
validates :name, presence: true
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
validates :password, presence: true, length: { minimum: 6 }
validates :password_confirmation, presence: true, length: { minimum: 6 }
before_save { |trainer| trainer.email = email.downcase }
end
数据库中的Client表有一个foreign_id trainer_id:
create_table "clients", :force => true do |t|
t.string "name"
t.string "email"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "password_digest"
t.integer "trainer_id"
end
在控制台中,当我尝试将客户记录添加到培训师时,无法保存到数据库:
1.9.3-p194 :001 > trainer = Trainer.find(2)
Trainer Load (30.1ms) SELECT "trainers".* FROM "trainers" WHERE "trainers"."id" = ? LIMIT 1 [["id", 2]]
=> #<Trainer id: 2, name: "Noah Macejkovic", email: "trainer-1@mp-trainer.com", created_at: "2012-06-06 20:40:45", updated_at: "2012-06-06 20:40:45", password_digest: "$2a$10$bDL0ealWYdsxpN2jOyToCO9T/WKsI4I7WXdhRXQBvcP1...">
1.9.3-p194 :002 > client = Client.find(2)
Client Load (0.2ms) SELECT "clients".* FROM "clients" WHERE "clients"."id" = ? LIMIT 1 [["id", 2]]
=> #<Client id: 2, name: "Katherine Skiles V", email: "client-1@example.com", created_at: "2012-06-06 20:40:54", updated_at: "2012-06-06 20:40:54", password_digest: "$2a$10$47CUSOS3k5j1c.DjH2PP7uqHD6kM94rZHMco37X34li/...", trainer_id: nil>
1.9.3-p194 :003 > trainer.clients << client
(0.1ms) begin transaction
Client Exists (0.4ms) SELECT 1 FROM "clients" WHERE (LOWER("clients"."email") = LOWER('client-1@example.com') AND "clients"."id" != 2) LIMIT 1
(0.1ms) commit transaction
=> false
如果我在交易后立即查询客户端,它会显示 trainer_id
1.9.3-p194 :004 > client
=> #<Client id: 2, name: "Katherine Skiles V", email: "client-1@example.com", created_at: "2012-06-06 20:40:54", updated_at: "2012-06-06 20:40:54", password_digest: "$2a$10$47CUSOS3k5j1c.DjH2PP7uqHD6kM94rZHMco37X34li/...", trainer_id: 2>
但是从数据库重新加载后,trainer_id 为 nil(正如预期的那样,因为从trainer.clients << client
.
1.9.3-p194 :006 > client.reload
Client Load (0.2ms) SELECT "clients".* FROM "clients" WHERE "clients"."id" = ? LIMIT 1 [["id", 2]]
=> #<Client id: 2, name: "Katherine Skiles V", email: "client-1@example.com", created_at: "2012-06-06 20:40:54", updated_at: "2012-06-06 20:40:54", password_digest: "$2a$10$47CUSOS3k5j1c.DjH2PP7uqHD6kM94rZHMco37X34li/...", trainer_id: nil>
所以问题的关键是“为什么提交到数据库失败?”