3

我有一个设置条目的表。我想在我的模型和控制器中将这些条目作为变量访问,而无需每次都查询数据库来设置这些变量。

我可以通过为我的模型和控制器创建重复的“关注点”来让它工作。我还可以在我的 ApplicationController 中设置全局变量。或者我可以在我需要它们的每个地方初始化它们。设置和访问可以在控制器和模型中访问的全局变量的正确 rails 方法是什么?

class ItemType
  has_many :items
end

class Item
  belongs_to :item_type
  belongs_to :foo
end

class Foo 
  has_many :items  

  def build_item
    bar_item_type = ItemType.find_by(:name => "bar")

    self.items.build(
      :foo_id => self.id,
      :item_type_id => bar_item_type.id
    )
  end
end

class ItemsController
  def update
    bar_item_type = ItemType.find_by(:name => "bar")

    @item.update(:item_type_id => bar_item_type.id)
  end

end

在示例中,您可以看到我bar_item_type在 Foo 模型和 ItemsController 中都声明了变量。我想通过能够为我的 rails 项目创建和访问该变量一次来干掉我的代码库,而不必在任何地方进行相同的数据库调用。

4

2 回答 2

1

我会反对这种硬编码或依赖于数据库状态的代码。如果你必须这样做,这是我知道的一种方法:

# models
class ItemType < ActiveRecord::Base
  has_many :items

  # caches the value after first call
  def self.with_bar
    @@with_bar ||= transaction { find_or_create_by(name: "bar") }
  end

  def self.with_bar_id
    with_bar.id
  end
end

class Item < ActiveRecord::Base
  belongs_to :item_type
  belongs_to :foo

  scope :with_bar_types, -> { where(item_type_id: ItemType.with_bar_id) }
end

class Foo < ActiveRecord::Base
  has_many :items  

  # automatically sets the foo_id, no need to mention explicitly
  # the chained with_bar_types automatically sets the item_type_id to ItemType.with_bar_id
  def build_item
    self.items.with_bar_types.new
  end
end

# Controller
class ItemsController
  def update
    @item.update(item_type_id: ItemType.with_bar_id)
  end
end
于 2019-07-17T20:42:16.713 回答
0

如果你必须使用常量,有几种方法可以做到。但是您必须考虑到您正在实例化一个 ActiveRecord 模型对象,该对象依赖于数据库中存在的数据。不建议这样做,因为您现在拥有依赖于数据库中存在的数据的模型和控制器逻辑。如果您已经为数据库播种并且它不会改变,这可能没问题。

class ItemType
  BAR_TYPE ||= where(:name => "bar").limit(1).first 

  has_many :items
end

现在无论你在哪里需要这个对象,你都可以这样称呼它:

bar_item_type  = ItemType::BAR_TYPE
于 2019-07-17T20:43:49.860 回答