简体   繁体   English

如何在Rails中使用模型范围调用静态函数?

[英]How do I call a static function with model scopes in Rails?

I wasn't sure how to get it to work syntax wise. 我不确定如何使其在语法上明智地工作。 I did get it to work like the following though: 我确实让它像以下那样工作:

  scope :out_of_stock, lambda{ |company| get_inventory(company, 'out_of_stock') }
  scope :in_stock, lambda{ |company| get_inventory(company, 'in_stock') }


  def self.get_inventory(company, stock_status)
    StockInventory.find_all_by_....
  end

However, is there a way to do it without the anonymous functions? 但是,有没有匿名功能的方法吗?

First of all, there are no static functions in Ruby. 首先,Ruby中没有静态函数。 The self.get_inventory is a class method, which is in effect a singleton instance method on the Company class. self.get_inventory是一个类方法,实际上是Company类上的单例实例方法。 The scope call is itself a class method that dynamically defines other class methods, in this case out_of_stock and in_stock . scope调用本身就是一个类方法,它动态定义其他类方法,在这种情况下为out_of_stockin_stock

Second, it's hard to tell what you're trying to do, since you didn't include your entire method, but I'll assume you're trying to get the instances of StockInventory that have a particular company id and inventory status. 其次,由于您没有包括整个方法,因此很难说出您要执行的操作,但是我假设您正在尝试获取具有特定公司ID和库存状态的StockInventory实例。

ActiveRecord allows you to use finders , as it appears you've done here, or relations , which are a bit more flexible, as they are typically chainable, and because their execution is delayed until absolutely necessary, meaning you can pass them around to different methods without hitting the database. ActiveRecord允许您使用查找器 (如您在此处所做的那样)或关系 ,因为它们通常是可链接的,所以它们更灵活一些,因为它们的执行被延迟到绝对必要,这意味着您可以将它们传递给其他对象方法,而无需访问数据库。 Unless I'm looking up a single object with a known id, I usually stick to relations for this reason. 除非我要查找具有已知ID的单个对象,否则通常会出于这种原因坚持使用关系。

I'm going to make a few assumptions, since your question isn't particularly clear. 我将作一些假设,因为您的问题不是特别清楚。 If you have defined a relation between Company and StockInventory (like has_many :stock_inventories and belongs_to :company ), you can use that relation as a starting point and defined a method like this on the StockInventory class: 如果您已定义CompanyStockInventory之间的关系(如has_many :stock_inventoriesbelongs_to :company ),则可以将该关系用作起点并在StockInventory类上定义如下方法:

def self.in_stock
  where(stock_status: "in stock") # or whatever
end

Note that instead of passing in a Company instance as an argument to a Company class method that ultimately loads some other class (this should be a red flag), you call this directly on the StockInventory class or any StockInventory relation. 请注意,您无需将Company实例作为参数传递给最终加载其他类的Company类方法(这应该是一个红旗),而是直接在StockInventory类或任何StockInventory关系上调用此方法。 Plus, since it's a relation, you can chain other ActiveRecord methods to it, ie my_company.stock_inventories.in_stock.limit(10) . 另外,由于它是一种关系,因此可以将其他ActiveRecord方法链接到该关系,即my_company.stock_inventories.in_stock.limit(10)

You'll need to alter this method to fit your particular database columns and statuses, but this is as complicated as it needs to get. 您需要更改此方法以适合您的特定数据库列和状态,但这很复杂。

Whatever the case, I recommend you read the Active Record Query Interface guide - there's a lot you can do with it once you understand how it works. 无论如何,我建议您阅读Active Record Query Interface指南-了解它的工作原理后,您可以做很多事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM