0

在 Rails 中,如何根据第一个属性自动分配项目的所有属性

我的问题的第一部分在上面的链接中,

我使用示例@ring.display_name = Ring::DISPLAY_NAME.sample 抓取了一个随机属性。这不是问题。困扰我的是抓住其余的属性。

我现在需要根据为 display_name 随机选择的内容来抓取 @ring.gold、@ring.roll 和 @ring.bonus。现在看一下我的模型,我知道这是错误的,但是您能告诉我是否有更好的方法可以做到这一点,也许可以吗?

class Ring < ActiveRecord::Base



  # Display_name
  DISPLAY_NAME = [ 'Beginners', 'Silver', 'Gold', 'Diamond' ]

  # Gold
  if Ring.display_name == 'Beginners'
    GOLD = [ 0 ]
  end
  if Ring.display_name == 'Silver'
    GOLD = [ 0 ]
  end
  if Ring.display_name == 'Gold'
    GOLD = [ 0 ]
  end
  if Ring.display_name == 'Diamond'
    GOLD = [ 0 ]
  end


  # Roll
  if @ring.display_name == 'Beginners'
    ROLL = [ 1 ]
  end  
  if @ring.display_name == 'Silver'
    ROLL = [ 1, 2, 3 ]
  end
  if @ring.display_name == 'Gold'
    ROLL = [ 2, 3, 4 ]
  end
  if @ring.display_name == 'Diamond'
    ROLL = [ 4, 5, 6 ]
  end

  # Bonus
  if Ring.display_name == 'Beginners'
    BONUS = [ ring.user.level + 1 ]
  end  
  if Ring.display_name == 'Silver'
    BONUS = [ ring.user.level + (1 + rand(3)) ]
  end
  if Ring.display_name == 'Gold'
    BONUS = [ ring.user.level + (1 + rand(5)) ]
  end
  if Ring.display_name == 'Diamond'
    BONUS = [ ring.user.level + (1 + rand(8)) ]
  end

  attr_accessible :description, :display_name, :roll, :bonus, :total, :image, :gold


end

正如您从代码中看到的那样,我尝试使用“if Ring.display_name”和“if @ring.display_name”这两种方法都不起作用,因为我假设任何阅读本文的人都知道他们不会。

谢谢!

更新!

这是我目前正在使用的代码。这是不对的,我不太明白如何使用这种技术。看看我有什么。

class Ring < ActiveRecord::Base

  # Display_name
  DISPLAY_NAME = [ 'Silver', 'Gold', 'Diamond' ]

  # Rolls
  ROLL = {
    'Silver'    => [1, 2, 3],
    'Gold'      => [2, 3, 4],
    'Diamond'   => [4, 5, 6]
  }
  self.roll = ROLL[self.display_name]

  BUY = {
    'Silver'    => [100..180],
    'Gold'      => [140..200],
    'Diamond'   => [180..350]
  }
  self.buy = BUY[self.display_name]

  BONUS = {
    'Silver'    => [(1 + rand(3))],
    'Gold'      => [(3 + rand(5))],
    'Diamond'   => [(5 + rand(8))]
  }
  self.bonus = BONUS[self.display_name].call

  attr_accessible :user, :active, :display_name, :description, :gold, :buy, :sell, :roll, :bonus, :total, :image, :description


end

在我的控制器中:

def create
    @ring = Ring.new
    @ring.display_name = Ring::DISPLAY_NAME.sample

    @ring.save

    redirect_to @ring
  end

我理解的方式,如果我告诉它选择一个随机的 display_name,所有其他字段将根据我在模型中的内容填写,对吗?

4

1 回答 1

0

您可以从条件句中生成哈希值,如下所示:

class Ring < ActiveRecord::Base

  # Display_name
  DISPLAY_NAME = [ 'Silver', 'Gold', 'Diamond' ]

  # Rolls
  ROLL = {
    'Silver'    => [1, 2, 3],
    'Gold'      => [2, 3, 4],
    'Diamond'   => [4, 5, 6]
  }

  BUY = {
    'Silver'    => [100..180],
    'Gold'      => [140..200],
    'Diamond'   => [180..350]
  }

  BONUS = {
    'Silver'    => ->(){(1 + rand(3))},
    'Gold'      => ->(){(3 + rand(5))},
    'Diamond'   => ->(){(5 + rand(8))}
  }

  attr_accessible :user, :active, :display_name, :description, :gold, :buy, :sell, :roll, :bonus, :total, :image, :description

  def display_name=(name)
    super(name)
    self.roll = ROLL[self.display_name]
    self.buy = BUY[self.display_name]
    self.bonus = BONUS[self.display_name].call
  end
end
于 2012-06-28T21:20:39.613 回答