Ruby on rails Rails attr_访问器在通过.build()作为参数传递后可发现
嗨,我在通过创建后回调获取属性访问器时遇到问题。正在通过父会议模型中的.build()创建“MeetingAttendee”。我感觉通过.build()创建MeetingAttendee子对象时,通过initialize传递的attr_访问器丢失了 如果我通过了“no_invite”参数,我将有效地防止after_create方法触发。如果您能在此了解Rails回调的复杂性,我将不胜感激 儿童会议与会者:Ruby on rails Rails attr_访问器在通过.build()作为参数传递后可发现,ruby-on-rails,ruby-on-rails-4,Ruby On Rails,Ruby On Rails 4,嗨,我在通过创建后回调获取属性访问器时遇到问题。正在通过父会议模型中的.build()创建“MeetingAttendee”。我感觉通过.build()创建MeetingAttendee子对象时,通过initialize传递的attr_访问器丢失了 如果我通过了“no_invite”参数,我将有效地防止after_create方法触发。如果您能在此了解Rails回调的复杂性,我将不胜感激 儿童会议与会者: class MeetingAttendee < ActiveRecord::
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
您只需要为回调使用正确的语法:
after_create :send_invitation!, unless: Proc.new { |a| a.no_invite }
较短的选择
after_create :send_invitation!, unless: :no_invite
谢谢@dgilperez,但这似乎不起作用。如果我检查发送邀请中的MeetingAttender对象!方法,我甚至没有看到no_invite参数。我不认为它能通过,我收回它。self检查不会显示attr_访问器,但打印该值会在类的任何方法中显示它。
after_create :send_invitation!, unless: :no_invite