我尝试正常更新属性,但它没有更新。
这是我的客户模型
class Customer < ActiveRecord::Base
attr_accessible :name, :phone_number, :email
attr_accessible :first_name
attr_accessible :last_name
before_validation :parse_name
def name
"#{first_name} #{last_name}".strip
end
private
def parse_name
if attributes['name'].present?
self.first_name, self.last_name = attributes['name'].strip.split(' ', 2)
end
end
end
我尝试更新first_name和last_name,但它没有更新,但是如果我尝试更新电子邮件则更新正常
这是我的rails控制台跟踪 更新电子邮件
2.1.0dev :004 > a = Customer.find(5)
Customer Load (0.2ms) SELECT `customers`.* FROM `customers` WHERE `customers`.`id` = 5 LIMIT 1
=> #<Customer id: 5, name: "First Last", phone_number: "1234567890", created_at: "2014-09-15 12:48:30", updated_at: "2014-09-15 14:32:17", first_name: "Fir
2.1.0dev :008 > a = Customer.find(5)
Customer Load (0.1ms) SELECT `customers`.* FROM `customers` WHERE `customers`.`id` = 5 LIMIT 1
=> #<Customer id: 5, name: "First Last", phone_number: "1234567890", created_at: "2014-09-15 12:48:30", updated_at: "2014-09-15 14:32:17", first_name: "Fir
st", last_name: "Last", email: "shri@mail.com">
2.1.0dev :009 > a.email = "Test@test.com"
=> "Test@test.com"
2.1.0dev :010 > a.save
(0.2ms) BEGIN
Customer Exists (0.2ms) SELECT 1 AS one FROM `customers` WHERE (`customers`.`phone_number` = BINARY '1234567890' AND `customers`.`id` != 5) LIMIT 1
(0.4ms) UPDATE `customers` SET `email` = 'Test@test.com', `updated_at` = '2014-09-15 20:38:31' WHERE `customers`.`id` = 5
(104.9ms) COMMIT
=> true
2.1.0dev :011 > a
=> #<Customer id: 5, name: "First Last", phone_number: "1234567890", created_at: "2014-09-15 12:48:30", updated_at: "2014-09-15 20:38:31", first_name: "Fir
st", last_name: "Last", email: "Test@test.com">
但是更新first_name并且last_name不起作用
更新last_name
2.1.0dev :012 > a.last_name = "last_name"
=> "last_name"
2.1.0dev :013 > a.save
(0.2ms) BEGIN
Customer Exists (0.7ms) SELECT 1 AS one FROM `customers` WHERE (`customers`.`phone_number` = BINARY '1234567890' AND `customers`.`id` != 5) LIMIT 1
(0.2ms) COMMIT
=> true
2.1.0dev :014 > a.save!
(0.2ms) BEGIN
Customer Exists (0.2ms) SELECT 1 AS one FROM `customers` WHERE (`customers`.`phone_number` = BINARY '1234567890' AND `customers`.`id` != 5) LIMIT 1
(0.1ms) COMMIT
=> true
2.1.0dev :015 > a
=> #<Customer id: 5, name: "First Last", phone_number: "1234567890", created_at: "2014-09-15 12:48:30", updated_at: "2014-09-15 20:38:31", first_name: "Fir
st", last_name: "Last", email: "Test@test.com">
使用更新属性
2.1.0dev :016 > a.update_attributes(:first_name => "test_name", :last_name => "test_name")
(0.2ms) BEGIN
Customer Exists (0.2ms) SELECT 1 AS one FROM `customers` WHERE (`customers`.`phone_number` = BINARY '1234567890' AND `customers`.`id` != 5) LIMIT 1
(0.2ms) COMMIT
=> true
2.1.0dev :017 > a
=> #<Customer id: 5, name: "First Last", phone_number: "1234567890", created_at: "2014-09-15 12:48:30", updated_at: "2014-09-15 20:38:31", first_name: "Fir
st", last_name: "Last", email: "Test@test.com">
我试图解决它,但它无法正常工作。
可以请告诉我我错过了什么。
我正在使用rails 3.2.14 and rails 2.1.0
答案 0 :(得分:2)
您有 before_validation 方法,其中根据名称设置名字和姓氏。您的记录已有名称,因此它会拆分此名称并提取名字和姓氏。
答案 1 :(得分:2)
您的before_validation
正在覆盖您要设置的值。我会一起删除解析名称方法,并使#name
方法只是基于first_name
和last_name
的计算值,例如
class Customer < ActiveRecord::Base
attr_accessible :phone_number, :email, :first_name, :last_name
def name
"#{first_name} #{last_name}".strip
end
end
如果你真的必须#name=
我会这样做
def name=(name_string)
self.first_name, self.last_name = name_string.strip.split(' ', 2)
end
您必须save
才能保留这些值。你也可以这样做。
def update_name(name_string)
update_attributes(Hash[[:first_name,:last_name].zip(name_string.strip.split(' ', 2))])
end
哪个会与save
一起运行,并根据有效性返回true
或false