Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.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方法中调用方法_Ruby On Rails_Ruby - Fatal编程技术网

Ruby on rails 如何在Rails方法中调用方法

Ruby on rails 如何在Rails方法中调用方法,ruby-on-rails,ruby,Ruby On Rails,Ruby,我正在努力调用我的用户模型中的方法。我有一个类似的长期方法: def find_content def find_x # call api end def find_y #call api end content = {"x": find_x, "y": find_y} return content end 然后我试着在我的模型中这样称呼它: class User < ApplicationRecord def User.news # get

我正在努力调用我的用户模型中的方法。我有一个类似的长期方法:

def find_content
  def find_x
  # call api
  end
  def find_y
  #call api
  end
  content = {"x": find_x, "y": find_y}
  return content
end
然后我试着在我的模型中这样称呼它:

class User < ApplicationRecord
  def User.news
    # get result of find_content
    content = find_content
    # I also tried doing User.find_content when the function was inside the model
    ## the function then passes the content variable to my UserMailer which sends emails to my users with the content
  end
class用户
我已尝试将我的查找内容放置在带有
def self.find_content
且不带self部分的用户模型中。
我想知道在模型中放置这样的函数的最佳位置是哪里。

如果我在哪里,我会创建一个服务类或lib类,并调用它

不要在方法中定义方法。试试这样的

class MyFancyService
  def find_content
    {"x": find_x, "y": find_y}
  end

  private
  def find_x
    #code
  end

  def find_y
    #code
  end
end

在你的模型里面

#remember to require your libs/services class in somewhere (maybe application.rb)
class User < ApplicationRecord
  def news
     MyFancyService.new.find_content    
  end
end
#记住在某个地方(可能是application.rb)需要libs/services类
类用户<应用程序记录
def新闻
MyFancyService.new.find_内容
终止
终止

不要滥用类方法(def self.bla),你应该有更多的实例方法。

出现这个问题的原因是,查找内容并不是用户真正关心的问题,应该像Horacio提到的那样分为一个单独的类,但我认为用户类不需要了解查找内容的任何内容。但是,您可能需要一些用户信息来正确查找内容

我建议这样做(假设您需要从用户对象调用api)

然后在控制器中有用户对象,因此从中获取所需内容,创建api实例并进行调用

user_stuff = @user.user_stuff_needed_by_api
news_api = NewsAPI.new(user_stuff)
content = news_api.find_content
如果您真的想在a用户实例中调用api(我认为您不应该这样做),我建议通过setter传入api实例,然后将find_内容委托给该实例。像这样

class User
  def set_news_api(api)
    @news_api = api
  end
  def find_content
     @news_api.find_content
  end
end
最后,如果你真的想把所有这些都放在用户类中,像这样的东西应该可以工作,但同样不推荐

class User
  def self.find_x
    "xx"
    # call api
    end
  def self.find_y
    "yy"
    #call api
  end
  def find_content  
    {"x": self.class.find_x, "y": self.class.find_y}
  end
  def self.other_find_content
    {"other_x": find_x, "other_y": find_y}
  end
  def user_stuff_needed_by_api
  end
end

puts User.new.find_content
puts User.other_find_content

尽量避免类方法“我正在努力调用一个方法”不是一个足够精确的错误描述,我们无法帮助您。什么不起作用?它怎么不起作用?你的代码有什么问题?你收到错误信息了吗?错误消息是什么?你得到的结果不是你期望的结果吗?你期望得到什么样的结果?为什么?你会得到什么样的结果?两者有什么不同?你观察到的行为是否不是期望的行为?期望的行为是什么,为什么,观察到的行为是什么,它们有什么不同?将find_content方法移动到一个单独的类中是好的,但是在每个需要它的方法中创建一个新实例是坏的。@nPn-我对“在每个需要它的方法中创建一个新实例是坏的”很感兴趣你有这方面的参考资料吗?或者,你能描述“坏”吗?“坏”就像“有点坏”。或者“坏”像“天空打开,把地球吞噬进一个深深的、永恒的黑暗深渊,那里除了哭泣和咬牙切齿什么都没有”坏?我想“坏”主要是因为需要找到内容的每一种方法都与我的FancyService紧密结合在一起。new()。如果MyFancyService中需要更改某些内容,比如说需要初始化方法中的某些参数,那么每个方法都需要更改。如果您希望使用不同服务类的find_content方法,或者甚至使用MyFancyService的模拟实例测试用户类,则同样适用于这种情况。因此,MyFancyService基本上是一个依赖项,如果需要的话可以将其注入类中,那就太好了。@nPn有很多方法可以避免这种情况(记忆化,或者您可以在初始化时创建一个实例并在之前使用它),但这取决于上下文。类方法看起来像一个糟糕的POO设计。我喜欢so+1,我试图解释他需要分成两类(至少),他不需要类方法。
class User
  def self.find_x
    "xx"
    # call api
    end
  def self.find_y
    "yy"
    #call api
  end
  def find_content  
    {"x": self.class.find_x, "y": self.class.find_y}
  end
  def self.other_find_content
    {"other_x": find_x, "other_y": find_y}
  end
  def user_stuff_needed_by_api
  end
end

puts User.new.find_content
puts User.other_find_content