ok, i’m about at that point in my ruby career where this should be tripping me up.
I have a model called distribution.rb where I have the follwoing protected method:
def update_email_sent_on_date
if self.send_to_changed?
self.date_email_delivered = DateTime.now
end
end
I then call this method from my controller:
distribution.update_email_sent_on_date
however, I’m getting this error:
NoMethodError (protected method `update_email_sent_on_date' called for #<EmailDistribution:0x131a1be90>):
the distribution object is indeed an EmailDistribution (a subclass of distribution where the method is defined). I thought this would work. In any case, I also tried moving the method to the subclass EmailDistribution but no luck. Same error message.
I’d also like to step back and say that what I’m trying to do overall is store the timestamp of when another field in the distribution model is updated. If there’s a simpler way, please enlighten me.
I think you’re getting tripped up because you are using the
protecteddeclaration when you actually want theprivatedeclaration.The
protectedterm in ruby acts differently in other conventional languages.This is a slightly clearer explanation IMHO, from the book Eloquent Ruby by Russ Olsen:
Lastly, it’s good to note that in ruby you can always call private or protected methods regardless of whether they are accessible by using the
sendmethod. So if you were in a pinch and just needed to work you could just call it like this and then worry about theprivate/protecteddeclarations later:Read this for more a better explanation..