Ruby on Rails:before_save字段为小写

时间:2022-12-22 01:20:59

I'm trying to change the fields from the form to lower case before they get saved in the database. This is my code but the output from the database is still in upper case why isnt the code working?

我想在将字段保存到数据库之前将字段从表单更改为小写。这是我的代码,但数据库的输出仍然是大写的,为什么代码不工作?

class Transaction < ActiveRecord::Base
   validates :name, presence: true
   validates :amount, presence: true, numericality: true
   before_save :downcase_fields

   def downcase_fields
      self.name.downcase
   end
end

5 个解决方案

#1


17  

downcase returns a copy of the string, doesn't modify the string itself. Use downcase! instead:

downcase返回字符串的副本,不会修改字符串本身。使用downcase!代替:

def downcase_fields
  self.name.downcase!
end

See documentation for more details.

有关详细信息,请参阅文档

#2


3  

String#downcase does not mutate the string, it simply returns a modified copy of that string. As others said, you could use the downcase! method.

String #downcase不会改变字符串,它只返回该字符串的修改后的副本。正如其他人所说,你可以使用羽绒服!方法。

def downcase_fields
  name.downcase!
end

However, if you wanted to stick with the downcase method, then you could do the following:

但是,如果您想坚持使用downcase方法,那么您可以执行以下操作:

def downcase_fields
  self.name = name.downcase
end

This reassigns the name instance variable to the result of calling downcase on the original value of name.

这会将名称实例变量重新分配给name的原始值上调用downcase的结果。

#3


1  

You need to use exclamation mark after calling method downcase, if you also want to save result of operation to the variable. So for you will be usable:

如果您还想将操作结果保存到变量中,则需要在调用方法downcase后使用感叹号。因此,您将可以使用:

self.name.downcase!

Don't forget that .downcase! replacement works only in ASCII region.

别忘了.downcase!替换仅适用于ASCII区域。

#4


1  

You're not setting name to downcase by running self.name.downcase, because #downcase does not modify the string, it returns it. You should use the bang downcase method

您没有通过运行self.name.downcase将名称设置为downcase,因为#downcase不会修改字符串,而是返回它。你应该使用bang downcase方法

self.name.downcase!

However, there's another way I like to do it:

但是,我喜欢这样做的另一种方式:

before_save: { name.downcase! }

#5


0  

Another solution is to remove the before_save and monkeypatch the initialize method

另一个解决方案是删除before_save和monkeypatch初始化方法

def initialize(args = {})
  args[:name].downcase! if args[:name]
  super
end

Then you can say something like

然后你可以这样说

irb(main)> t = Transaction.new name: 'BIGTRANSACTION'

irb(main)> t.name

=> "bigtransaction

#1


17  

downcase returns a copy of the string, doesn't modify the string itself. Use downcase! instead:

downcase返回字符串的副本,不会修改字符串本身。使用downcase!代替:

def downcase_fields
  self.name.downcase!
end

See documentation for more details.

有关详细信息,请参阅文档

#2


3  

String#downcase does not mutate the string, it simply returns a modified copy of that string. As others said, you could use the downcase! method.

String #downcase不会改变字符串,它只返回该字符串的修改后的副本。正如其他人所说,你可以使用羽绒服!方法。

def downcase_fields
  name.downcase!
end

However, if you wanted to stick with the downcase method, then you could do the following:

但是,如果您想坚持使用downcase方法,那么您可以执行以下操作:

def downcase_fields
  self.name = name.downcase
end

This reassigns the name instance variable to the result of calling downcase on the original value of name.

这会将名称实例变量重新分配给name的原始值上调用downcase的结果。

#3


1  

You need to use exclamation mark after calling method downcase, if you also want to save result of operation to the variable. So for you will be usable:

如果您还想将操作结果保存到变量中,则需要在调用方法downcase后使用感叹号。因此,您将可以使用:

self.name.downcase!

Don't forget that .downcase! replacement works only in ASCII region.

别忘了.downcase!替换仅适用于ASCII区域。

#4


1  

You're not setting name to downcase by running self.name.downcase, because #downcase does not modify the string, it returns it. You should use the bang downcase method

您没有通过运行self.name.downcase将名称设置为downcase,因为#downcase不会修改字符串,而是返回它。你应该使用bang downcase方法

self.name.downcase!

However, there's another way I like to do it:

但是,我喜欢这样做的另一种方式:

before_save: { name.downcase! }

#5


0  

Another solution is to remove the before_save and monkeypatch the initialize method

另一个解决方案是删除before_save和monkeypatch初始化方法

def initialize(args = {})
  args[:name].downcase! if args[:name]
  super
end

Then you can say something like

然后你可以这样说

irb(main)> t = Transaction.new name: 'BIGTRANSACTION'

irb(main)> t.name

=> "bigtransaction