Reputation: 21261
The relevant bits:
class Event < ActiveRecord::Base
...
belongs_to :owner, class_name: User, foreign_key: :owner_id
...
def self.lookup_and_send_emails(addresses, msg)
addresses.each do |email|
user = User.where(email: :email)
if user
invite!( user)
SwapMailer.babysitter_request(email, msg).deliver
else
#send the devise/website invitation
end
end
end
def self.invite!(user)
UsersEventInvitation.create(invite_sent_by: self.owner.id, invite_sent_to: user.id, event_id: self.id)
end
...
end
I'm getting an exception:
undefined method `owner' for #<Class:0x007fa0dcd73fc8>
what gives here? What I'd like to do is assign the current_user.id
where i'm using self.owner.id but I thought this would be a workaround since that wasn't working.
UPDATE:
I was calling Event.lookup_and_send_emails(...)
from my controller. I'm now doing @event.lookup_and_send_emails(...)
, however I now get undefined method
id' for #pointing at the the
self.owner.idline in the
invite!` method.
Upvotes: 0
Views: 63
Reputation: 332
Remove self from invite! And as well as lookup_and_send_emails method. As they should instance methods. Appending self makes them class method. And when u call other method within a class method without any object it takes it as class method only.
class Event < ActiveRecord::Base ...
belongs_to :owner, class_name: User, foreign_key: :owner_id
def lookup_and_send_emails(addresse s, msg) addresses.each do |email| user = User.where(email: :email) if user invite!( user)
SwapMailer.babysitter_request(email, msg).deliver else #send the devise/website invitation end end end
def invite!(user)
UsersEventInvitation.create(invite_sent_by: self.owner.id, invite_sent_to: user.id, event_id: self.id)
end
Hope it helps .
Upvotes: 1
Reputation: 44962
The problem is that the invite!
method is a class method, not an instance method. So, it is defined on the Class
object representing the Event
and not any one individual event. If you are looking to send an invite from the owner of a particular event, these need to be instance methods. so define them with
def invite!(user)
e.g. no self.
preceding the method definition.
Upvotes: 1