嗨,我很难通过一个attr_accessor回调获得一个after_create。“MeetingAttendee”是通过父会议模型中的.build()创建的。我有一种感觉,当通过初始化传递attr_accessor时,通过.build()创建MeetingAttendee子对象时会丢失。
如果我通过“after_create”arg,我将有效地防止no_invite方法的触发。希望能在这里接受任何有关Rails回调的复杂知识的教育。
儿童MeetingAttendee:
class MeetingAttendee < ActiveRecord::Base
before_create :create_permalink!
after_create :send_invitation! if Proc.new { |a| a.no_invite.nil? }
attr_accessor :no_invite
belongs_to :meeting
end作为参数传递no_invite attr_accessor值的父会议。
self.attendees.build(
first_name: [attendee_data['first_name'], attendee_data['middle_name']].compact.join(' '),
last_name: attendee_data['last_name'],
attendee_email: attendee_data['email'],
profile_picture_url: profile_picture_url,
no_invite: true
)发布于 2015-02-19 00:33:06
您只需要为回调使用正确的语法:
after_create :send_invitation!, unless: Proc.new { |a| a.no_invite }较短的备选方案
after_create :send_invitation!, unless: :no_invitehttps://stackoverflow.com/questions/28596644
复制相似问题