我的红宝石模型中有一个如下所示的方法:
def testing
update_stock = Stock::Detail.find(stock) # (Assume I have a stock id.)
update_stock.update_attributes(details: "dashboard")
end
以上工作正常。但是如果的details
值已经像'table,chair'
一样string
。如何将附加'dashboard'
到details
字段中。当前,update_attributes
删除旧值并更新新值。我想将值附加到现有值。
如果我将字符串值声明为变量
detail_value = 'Code: 123AB, dashboard, code: 1235Q, table, chair'
def testing
update_stock = Stock::Detail.find(stock) # (Assume I have a stock id.)
update_stock.update_attributes(details: detail_value)
end
我想追加details
以上code
不存在的列。
例如,我的详细信息列的值如下
'Code: 123AB, dashboard, code: 1235Q, table, chair' # Before
我的参数是 'Code:123AB, dashboard' # Parameter
我想parameter
从string
值中删除并更新到数据库列。
所以我的column
价值是'code: 1235Q, table, chair'
谢谢
为什么不这样:
def testing
update_stock = Stock::Detail.find(stock)
update_stock.details += 'dashboard'
update_stock.save
end
更新答案:可能检查此字符串是否出现在details
:
update_stock.details += 'dashboard' unless update_stock.details =~ /dashboard/
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句