Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 更新模型的所有道具_Ruby On Rails_Ruby - Fatal编程技术网

Ruby on rails 更新模型的所有道具

Ruby on rails 更新模型的所有道具,ruby-on-rails,ruby,Ruby On Rails,Ruby,我的应用程序有以下几种型号 class Metric < ApplicationRecord belongs_to :post def analyse! client = ThirdPartyService.new res = client.get_metrics post.body # how to update itself here with the result of res? end end 以及调用的返回结果: client = Thir

我的应用程序有以下几种型号

class Metric < ApplicationRecord
  belongs_to :post

  def analyse!
    client = ThirdPartyService.new
    res = client.get_metrics post.body
    # how to update itself here with the result of res?
  end
end
以及调用的返回结果:

client = ThirdPartyService.new
res = client.get_metrics post.body
是一个包含以下内容的哈希:

{
  :adverbs_percentage => 10
  :sentiment => 'Positive'
}
我想知道如何从
分析更新度量方法,类似于:

update_myself(res)
谢谢。

我相信你能做到:

class Metric < ApplicationRecord
  belongs_to :post

  def analyse!
    client = ThirdPartyService.new
    res = client.get_metrics post.body
    update res
  end
end
class Metric
我相信你可以做到:

class Metric < ApplicationRecord
  belongs_to :post

  def analyse!
    client = ThirdPartyService.new
    res = client.get_metrics post.body
    update res
  end
end
class Metric
根据Rails,您可以通过以下方式更新您的模型

m=Metric.first
m、 副词的百分比=10
m、 情绪=‘积极’
m、 拯救
在上面的例子中,
副词\u percentage=
情绪=
保存
度量
类的实例方法。
您通过继承
ApplicationRecord
定义了这些实例方法

因为它们是实例方法,所以可以在实例方法中调用这些方法(
analysis!

class Metric
注意,
副词\u百分比=
情绪=
需要
self.
,因为ruby需要将它们与局部变量赋值区分开来。
有关更多详细信息,请参阅。

根据Rails,您可以通过以下方式更新您的模型

m=Metric.first
m、 副词的百分比=10
m、 情绪=‘积极’
m、 拯救
在上面的例子中,
副词\u percentage=
情绪=
保存
度量
类的实例方法。
您通过继承
ApplicationRecord
定义了这些实例方法

因为它们是实例方法,所以可以在实例方法中调用这些方法(
analysis!

class Metric
注意,
副词\u百分比=
情绪=
需要
self.
,因为ruby需要将它们与局部变量赋值区分开来。
有关更多详细信息,请参阅

class Metric < ApplicationRecord
  belongs_to :post

  def analyse!
    client = ThirdPartyService.new
    res = client.get_metrics post.body
    update res
  end
end