daxiang28
daxiang28

Reputation: 553

Rails attr_accessor discoverable after passing as argument via .build()

Hi I am having trouble getting an attr_accessor via an after_create callback. The 'MeetingAttendee' is being created via a .build() in the parent Meeting model. I have a feeling that the attr_accessor when passed via the initialize is being lost when creating the MeetingAttendee child object via .build().

I am effectively looking to prevent an after_create method from firing if I pass the 'no_invite' arg. Would appreciate any education on the intricacies of Rails callbacks here.

Child 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

Parent Meeting passing the no_invite attr_accessor value as an argument.

      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
      )

Upvotes: 0

Views: 428

Answers (1)

dgilperez
dgilperez

Reputation: 10796

You just need to use the right syntax for your callback:

after_create :send_invitation!, unless: Proc.new { |a| a.no_invite }

A shorter alternative

after_create :send_invitation!, unless: :no_invite

Upvotes: 2

Related Questions