当前位置 : 主页 > 网络编程 > 其它编程 >

Rails不能从记录中删除“\r\n”。-RailsCan'tremove“\r\n”fromrecord

来源:互联网 收集:自由互联 发布时间:2023-07-02
IveimportedsomedatacomCSVfilesandendedupwithafewrecordscontainingthestring\r\ni I've imported some data com CSV files and ended up with a few records containing the string "\r\n" in a column. When exporting to a CSV again, these lines screw
IveimportedsomedatacomCSVfilesandendedupwithafewrecordscontainingthestring\r\ni

I've imported some data com CSV files and ended up with a few records containing the string "\r\n" in a column. When exporting to a CSV again, these lines screw up the lines as they insert a new line where it should not...

我导入了一些data com CSV文件,最后得到了一些记录,其中包含列中的字符串“\r\n”。当再次导出到CSV时,这些行会把这些行搞砸,因为它们插入了一个不应该……

I've tried to remove with a Rake task, but it seems that ActiveRecord is not issuing an UPDATE query and I can't figure out what I'm doing wrong...

我尝试用Rake任务删除,但是好像ActiveRecord没有发出更新查询,我也不知道我做错了什么……

This is what I'm doing:

这就是我正在做的:

Contact.all.each {|c| next if c.address.nil? || !c.address.include?("\r\n"); c.address.gsub!("\r\n", " - "); c.save; }

This is the output from a rails c -s session:

这是rails c -s会话的输出:

(1.7ms) SAVEPOINT active_record_1(0.1ms) RELEASE SAVEPOINT active_record_1(0.0ms) SAVEPOINT active_record_1(0.0ms) RELEASE SAVEPOINT active_record_1(0.1ms) SAVEPOINT active_record_1(0.1ms) RELEASE SAVEPOINT active_record_1(0.0ms) SAVEPOINT active_record_1(0.0ms) RELEASE SAVEPOINT active_record_1

There is no UPDATE issued...

没有更新发布…

Any ideas of why it's not working?

你知道它为什么不工作吗?

2 个解决方案

#1

2

Your problem is that gsub! modifies the string in-place:

你的问题是那辆gsub!就地修改字符串:

gsub!(pattern, replacement) → str or nil gsub!(pattern) {|match| block } → str or nil gsub!(pattern) → an_enumerator

gsub !(替换模式)→str或零gsub !(模式){ | |块匹配}→str或零gsub !(模式)→an_enumerator

Performs the substitutions of String#gsub in place, returning str, or nil if no substitutions were performed. [...]

执行字符串#gsub的替换,如果没有执行替换,返回str或nil。[…]

gsub is quite happy to have a String as its first argument:

gsub很高兴有一个字符串作为它的第一个参数:

gsub(pattern, replacement) → new_str gsub(pattern, hash) → new_str gsub(pattern) {|match| block } → new_str gsub(pattern) → enumerator

gsub(模式、更换)→new_str gsub(模式、散列)→new_str gsub(模式){ | |块匹配}→new_str gsub(模式)→枚举器

[...] The pattern is typically a Regexp; if given as a String, any regular expression metacharacters it contains will be interpreted literally [...]

[…该模式通常是一个Regexp;如果以字符串形式给出,它所包含的任何正则表达式元字符都将按字面解释[…]

So s.gsub!("\r\n", ' - ') and s.gsub!(/\r\n/, ' - ') will have exactly the same effect.

所以s.gsub !(“\r\n”、“-”)及s.gsub!(/\r\n/, ' - ')将产生完全相同的效果。

So what happens when you use gsub!? If you do this:

那么使用gsub会发生什么呢?如果你这样做:

c.address.gsub!("\r\n", " - ")

you change c.address in a way that ActiveRecord won't recognize. For example, try this in the Rails console:

你改变c。以一种ActiveRecord无法识别的方式处理地址。例如,在Rails控制台尝试以下操作:

> c = Address.find(some_valid_id)> c.address.gsub!('e', 'x') # Assuming that the address has an 'e' in it of course...> c.changed? => false> c.address_changed? => false

So you've changed the address string but ActiveRecord won't know because c.address will still be the same String object. Since ActiveRecord doesn't think anything has changed, c.save won't do anything.

你改变了地址字符串但是ActiveRecord不知道因为c。地址仍然是相同的字符串对象。因为ActiveRecord不认为有任何改变,所以c。保存不会做任何事情。

If you switch to the gsub version:

如果您切换到gsub版本:

c.address = c.address.gsub("\r\n", ' - ')

then you will be replacing c.address with a whole new String and c.address_changed? and c.changed? will both be true. Now ActiveRecord will recognize that you've changed c and c.save (or c.save!) will send an UPDATE into your database.

然后你要替换c。用一个全新的字符串和c.address_changed地址?和c.changed吗?都将是真实的。现在ActiveRecord会发现您已经更改了c和c。save(或c.save!)将向数据库发送更新。

Note that gsub! sometimes returning nil is wholly irrelevant here, nothing in your code looks at what gsub! returns so it doesn't matter what it returns.

请注意,gsub !有时返回nil完全无关紧要,代码中没有任何东西可以查看gsub!返回所以它返回什么并不重要。


I'd probably do this sort of thing right inside the database with SQL but the specifics of how you'd do that depend on the underlying database. I cringe whenever my I want to say Model.all because I'm used to dealing with big databases where using all is just a convenient way to thrash your memory.

我可能会在数据库中使用SQL做这类事情,但是具体如何做取决于底层数据库。每当我想说“模特”时,我就会畏缩。所有这一切都是因为我习惯于处理大型数据库,在这些数据库中,使用all只是一种方便的方式来刷新内存。

#2

1

Use String.encode(universal_newline: true) instead gsub. It converting CRLF and CR to LF

使用字符串。编码(universal_newline:真)而不是gsub。它将CRLF和CR转换为LF

网友评论