不能在ruby代码中保持字符串中的反斜杠

我的ruby代码中有一个字符串,以反斜杠结尾:

acc.secret_key = "ASJSJJDSJFJJFFJJFJF\" acc.save 

上面是代码片段,当我尝试使用Active记录保存它时出现错误,我尝试添加另一个斜杠,

 acc.secret_key = "ASJSJJDSJFJJFFJJFJF\\" acc.save 

但现在我在DB中有两个斜杠。 我错过了什么? 非常感谢。

你在控制台看到这个吗? 如果是这种情况,你只是看到逃避不是两个实际的反斜杠。

 string = "1234\\" # => "1234\\" string.length # => 5 (if there were two \\'s the length would be 6) string # => "1234\\" puts string # 1234\ # => nil 

如果在db控制台中查找带有转义反斜杠的记录,则应该看到一个反斜杠。

 tests_development=> select * from tests WHERE tests.id = 1; id | name | created_at | updated_at | public ----+-----------------+----------------------------+----------------------------+-------- 1 | this is a test\ | 2013-02-05 21:44:12.339854 | 2013-02-05 21:44:12.339854 | t (1 row) 
 acc.secret_key = "ASJSJJDSJFJJFFJJFJF//".to_string acc.save 

这会将带有backshash的字符串推送到数据库中