Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/67.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 Rails 3如何查找或创建触发的动作_Ruby On Rails_Ruby On Rails 3_Activerecord - Fatal编程技术网

Ruby on rails Rails 3如何查找或创建触发的动作

Ruby on rails Rails 3如何查找或创建触发的动作,ruby-on-rails,ruby-on-rails-3,activerecord,Ruby On Rails,Ruby On Rails 3,Activerecord,我只是遇到了一些我必须做的事情 find_or_create 或 正如建议的那样 但有件事我一直在想 有没有办法确定触发了哪个动作?(因为我想在创建时显示适当的通知)顺便说一句,我使用的是Ruby 1.9.3 提前感谢。如果ruby是1.9.3+ if ['new','include'].include? __method__ then ... else ... end 我个人的想法不是使用find\u或\u create,而是分别使用find和create,这样我认为它更干净、更可

我只是遇到了一些我必须做的事情

find_or_create

正如建议的那样

但有件事我一直在想

有没有办法确定触发了哪个动作?(因为我想在创建时显示适当的通知)顺便说一句,我使用的是Ruby 1.9.3


提前感谢。

如果ruby是
1.9.3+

if ['new','include'].include? __method__ then
  ...
else
  ...
end

我个人的想法不是使用
find\u或\u create
,而是分别使用find和create,这样我认为它更干净、更可扩展

例如,如果您想在创建记录后再做一些事情。我坚信一种方法的概念应该做一件事:)

所以我会写这样的东西


又是你,迈克尔。谢谢你的回答,你能告诉我更多关于如何在控制器中使用它的细节吗?谢谢michael,现在看来shweta的方法更短了。但我会记住这个谢谢。我也考虑过使用if-else,但我对使用
find\u或\u create
first\u或\u create
时的动作有点感兴趣,我曾尝试按照Michael的建议使用
\uu方法,但我对ruby有点陌生,无法找到解决方案。我想我可能会同意shweta的答案。你说得有道理。谢谢
if ['new','include'].include? __method__ then
  ...
else
  ...
end
if Model.find_by_attributes(attributes)
  flash[:notice] = "record found"
else
  Model.create(attributes)
  flash[:notice] = "record created"
end