【问题标题】:first_or_initialize create only one objectfirst_or_initialize 只创建一个对象
【发布时间】:2014-03-17 09:30:51
【问题描述】:

我对 rails first_or_initialize 方法有疑问。我有模型本地化,它有_many OpeningHours。在我的模型本地化中,我有代码:

has_many :opening_hours
  accepts_nested_attributes_for :opening_hours, reject_if: proc { |attributes| attributes['from_time'].blank? }, allow_destroy: true
  after_initialize :build_opening_hours

  def build_opening_hours
    if self.new_record? 
      self.opening_hours.first_or_initialize(day: "monday", from_time: nil, to_time: nil)
      self.opening_hours.first_or_initialize(day: "tuesday", from_time: nil, to_time: nil)
    end
  end

问题是这个 build_opening_hours 方法只用 day: "monday" 初始化对象,第二个用 day: "tuesday" 的对象没有初始化。怎么了?我想不通这是为什么...

【问题讨论】:

  • 没人知道答案吗?这对我来说非常重要。请帮忙...
  • 您如何验证它是否仅初始化第一个对象,即将日期记录为“星期一”?
  • Mateusz,关联通过association_id 键识别。因此,如果您想获取 resource.opening_hours,您将一无所获,因为您的新资源记录还没有 id。请提供一个模型和你的目标,我会尽力帮助你

标签: ruby-on-rails ruby-on-rails-4 ruby-on-rails-3.2


【解决方案1】:

感谢所有试图帮助我的人。我有模型本地化:

class Localization < ActiveRecord::Base
has_many :opening_hours
  accepts_nested_attributes_for :opening_hours, reject_if: proc { |attributes| attributes['from_time'].blank? }, allow_destroy: true
  after_initialize :build_opening_hours

  def build_opening_hours
    if self.new_record? 
      self.opening_hours.first_or_initialize(day: "monday", from_time: nil, to_time: nil)
      self.opening_hours.first_or_initialize(day: "tuesday", from_time: nil, to_time: nil)
    end
  end
end

这个模型有很多opening_hours。我的模型 opening_hours 如下所示:

class OpeningHour < ActiveRecord::Base
belongs_to :localization
validates :to_time, :from_time, presence: true
end

现在我的目标是当我尝试创建新的本地化以使 7 个 opening_hours 对象与此本地化相关联时,例如:“monday”、“tuesday”等。 我创建新本地化的表单如下所示:

= simple_form_for [:partners, @localization] do |f|
  = f.input :name
  = f.input :address
  = f.input :city
  = f.input :zip_code
  = f.input :phone_in_hours
  = f.input :phone_after_hours
  = f.input :email 
  = f.input :email2
  = f.input :substitution_cost_in_hours
  = f.input :return_cost_in_hours
  #checkbox
    = check_box_tag 'after_hours_checkbox', 1, true
    %b= t('localizations.after_hours')
  .disabled
    = f.input :return_cost_after_hours
    = f.input :substitution_cost_after_hours
    = f.input :information
    %table.table.table-striped.table-bordered#localizations
      %thead
        %tr
          %th Czynne
          %th Dzień tygodnia
          %th Otwarte od
          %th Otwarte do 
      %tbody
        = f.simple_fields_for :opening_hours do |t|
          %tr    
            %td
            %td
              = t.input :day
            %td
              = t.input :from_time, as: :string, :input_html => { :class => 'from_time' } 
            %td
              = t.input :to_time, as: :string, :input_html => { :class => 'to_time' } 

  = f.submit t('localizations.add'), class: "btn btn-primary"

【讨论】:

    猜你喜欢
    • 2021-12-30
    • 1970-01-01
    • 2010-09-22
    • 1970-01-01
    • 2012-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多