0

我正在尝试在更新嵌套属性表单后发送电子邮件通知,但我一直遇到此错误,我认为这是因为我在委员会模型更新数据库中的对象之前发送电子邮件通知,因此得到一个 nil对于 Property.councils.email,任何帮助将不胜感激。

物业邮件

class PropertyMailer < ActionMailer::Base
  default from: "myemail@gmail.com"

  def welcome_email(property, tenants, councils)
    @property = property
    @tenants = tenants
    @councils = property.councils
    mail(:to => property.councils.first.email, subject: 'Here are your property details')
  end
end

属性.rb

class Property < ActiveRecord::Base

has_many :council_histories
  accepts_nested_attributes_for :council_histories, :reject_if => :send_email


  has_many :councils, through: :council_histories, :foreign_key => :council_id
  accepts_nested_attributes_for :councils

def send_email
    if council_histories.where(council_id_changed?)
      PropertyMailer.welcome_email(self, tenants, councils).deliver
    end
  end
end

更新 #

'属性/构建控制器'嵌套控制器,正在使用邪恶的向导表单 gem,构建多步骤表单。

class Properties::BuildController < ApplicationController
  include Wicked::Wizard

   steps :tenant, :meter, :council, :confirmed 

  def show
    @property = Property.find(params[:property_id])
    @tenants = @property.tenants.new(params[:tenant_id])
    @meter = @property.build_meter
    @property.council_histories.build do |council_history| 
    @council = council_history.build_council 
    end
    render_wizard
  end


  def update
    @property = Property.find(params[:property_id])
    params[:property][:status] = step.to_s
    params[:property][:status] = 'active' if step == steps.last
    @property.update_attributes(params[:property])
    render_wizard @property
  end
end

表单视图

<%= simple_form_for @property, url: wizard_path, :method => 'put' do |f|  %>


  <%= f.simple_fields_for :council_histories do |builder| %>
      <%= builder.input :property_id, as: :hidden, input_html: { value: @property.id } %> 
      <%= builder.input :council_id, :collection => Council.all  %>

      <%= builder.submit %>
    <% end %>
<% end %>
4

2 回答 2

1

正如 Marek Lipka 所说,property.councils 很可能返回一个空哈希,但这只是故事的一部分。我相信问题出在你的Property模型中,这条线:

has_many :council_histories
accepts_nested_attributes_for :council_histories, :reject_if => :send_email
                                                   ^^
                                                   This is the problem here

您最初的假设是正确的,我相信您是在尝试在:councils关系有机会填充之前发送电子邮件。顾名思义,该:reject_if方法用于在某些情况下丢弃数据(我自己从不使用它,所以想不出任何好的例子,但我相信有很多)。在这里查看更多信息

您是否绝对需要在对象被持久化之前发送电子邮件?如果没有,也许另一种选择是使用ActiveRecord::Callback方法之一,例如after_commit,像这样:

class Property < ActiveRecord::Base
    after_commit :send_email
    # Remainder of model code....
于 2013-08-23T16:41:59.677 回答
0

您的property.councils调用返回空集。

于 2013-08-23T15:47:52.480 回答