打印模型方法中的变量
Print variables coming to model's methods
有以下代码:
class MyModel < ActiveRecord::Base
before_create :do_process
def do_process
#I want to print here everything that came to this method
self.my_stuff = my_stuff
end
假设我们有具有 name
和 description
属性的模型,并且
我要进入控制台并输入类似
的内容
MyModel.create! name: 'test', description: 'test'
那么如何查看传递给方法 do_process
的参数?
你可以这样做:
def do_process(*args)
puts args
# body of method
end
看来你真正想要的是在对象进入do_process
方法时看到对象的属性。为此,您可以使用 attributes
方法:
def do_process
$stderr.puts attributes
self.my_stuff = my_stuff
end
有以下代码:
class MyModel < ActiveRecord::Base
before_create :do_process
def do_process
#I want to print here everything that came to this method
self.my_stuff = my_stuff
end
假设我们有具有 name
和 description
属性的模型,并且
我要进入控制台并输入类似
MyModel.create! name: 'test', description: 'test'
那么如何查看传递给方法 do_process
的参数?
你可以这样做:
def do_process(*args)
puts args
# body of method
end
看来你真正想要的是在对象进入do_process
方法时看到对象的属性。为此,您可以使用 attributes
方法:
def do_process
$stderr.puts attributes
self.my_stuff = my_stuff
end