0

所以我有一个随机项目生成器,这里是 ring.rb 的代码

class Ring < ActiveRecord::Base

  belongs_to :user

  after_initialize :randomly_make_ring

  def randomly_make_ring
    #everything is figured during initialization
    #You can call instance.make_ring to do it at any time.

    #chance calculation for description
    random_description = rand(1000)

    case random_description
    when 0..500
      self.description = 'Silver'
    when 501..800
      self.description = 'Gold'
    when 801..1000
      self.description = 'Diamond'
    end

    #Chance Calculation for Quality
    random_quality = rand(1000)

    case random_quality
    when 0..500
      self.quality = 'Sturdy'
    when 501..800
      self.quality = 'Elegant'
    when 801..1000
      self.quality = 'Platinum'
    end

    # Attributes according to the description
    case description
    when 'Silver'
      self.min_guard_roll = 1
      self.max_guard_roll = 2 + rand(4)
      self.min_pet_roll = 0
      self.max_pet_roll = 1
      self.min_trap_roll = 0
      self.max_trap_roll = 2
      self.buy = 20 + rand(15)      
    when 'Gold'
      self.min_guard_roll = 2
      self.max_guard_roll = 3 + rand(7)
      self.min_pet_roll = 1
      self.max_pet_roll = 3
      self.min_trap_roll = 1
      self.max_trap_roll = 3
      self.buy = 30 + rand(25)
    when 'Diamond'
      self.min_guard_roll = 4
      self.max_guard_roll = 5 + rand(10)
      self.min_pet_roll = 2
      self.max_pet_roll = 5
      self.min_trap_roll = 2
      self.max_trap_roll = 6
      self.buy = 80 + rand(48)
    end   

    # Attributes according to the quality
    case quality
    when 'Sturdy'
      self.min_guard_roll = self.min_guard_roll + 1
      self.max_guard_roll = self.max_guard_roll + (1 + rand(2))
      self.buy = self.buy + (10 + rand(6))   
    when 'Elegant'
      self.min_guard_roll = self.min_guard_roll + 2
      self.max_guard_roll = self.max_guard_roll + (2 + rand(3))
      self.buy = self.buy + (25 + rand(14))  
    when 'Platinum'
      self.min_guard_roll = self.min_guard_roll + 4
      self.max_guard_roll = self.max_guard_roll + (4 + rand(8))
      self.buy = self.buy + (45 + rand(25))  
    end

    # Generate display name
    self.display_name = quality + ' ' + description + ' Ring'

    # Generate Sell Value
    self.sell = self.buy * ((10 + rand(20)).to_f / (31 + rand(20)).to_f).to_f



  end

  attr_accessible :user, :active, :display_name, :description, :buy, :sell, :min_guard_roll, :max_guard_roll, :min_trap_roll, :max_trap_roll, :min_pet_roll, :max_pet_roll, :image

结尾

现在这是我设置的控制器和操作:

def create

@ring = Ring.new
@ring.user = current_user

@ring.save

@user = current_user
@user.gold = @user.gold - 25 
@user.save

redirect_to ring_path(:id => @ring.id)


end  

def show
    @ring = Ring.find(params[:id])
  end

我遇到的问题是,每当创建一个环时,它都会将我带到显示页面的页面,但环的属性与保存到数据库的属性不同。所以它没有抓住正确的戒指,由于我不明白的原因,每次显示或刷新页面时都会生成一个新的戒指。在我调出@ring 以显示其属性的任何页面上都会发生同样的事情。

我希望这个工作的确切方式是,用户可以寻找一个戒指,然后会弹出一个随机戒指,他们可以选择购买它或查看不同的戒指。如果他们选择购买它,它就在那里,如果他们选择查看不同的,当前的会被销毁。

有人可以帮我理解为什么会发生这种情况以及是否有更好的设置方法吗?谢谢!

4

1 回答 1

0

after_initialize每次实例化对象时都会运行回调,无论您是Ring.new使用find. 就目前而言,每当您从数据库中获取对象时,您都会立即用随机生成的属性覆盖其所有属性。

您可以将回调更改为 a before_create,这将导致它在首次保存对象时运行,或者根本不使其成为回调,仅在需要时调用它。

于 2012-07-08T14:59:23.513 回答