【问题标题】:Rails 3.1 - Conditional statements in a model?Rails 3.1 - 模型中的条件语句?
【发布时间】:2012-05-18 00:54:12
【问题描述】:

是否可以在 Rails 模型中使用条件语句?

我在想这样的事情:

if create
  before_save do
  self.position = self.track.position
  end
else
  acts_as_list :scope => :product_id
end

我基本上希望acts_as_list 在初始创建完成后生效。

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3


    【解决方案1】:

    您似乎正在寻找 before_create。

    class Model < ActiveRecord::Base
    
      acts_as_list :scope => :product_id
      before_create :set_initial_position
    
      private
    
      def set_initial_position
        self.position = self.track.position
      end
    end
    

    【讨论】:

      猜你喜欢
      • 2010-11-04
      • 1970-01-01
      • 1970-01-01
      • 2016-06-25
      • 1970-01-01
      • 1970-01-01
      • 2016-04-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多