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