As part of learning ruby/rails, I'm trying to implement http://github.com/professionalnerd/simple-private-messages into my application from scratch, instead of just installing the plugin. (I know, causing myself trouble but it's a good learning experience.)
I created the model, and the associations seem ok and I can create new messages fine and they show up in the recipients mailbox. But if I click to view one message (calls the show method in the message controller) it trips up on looking for a method called 'read' eg.
undefined method `read' for #<Class:0xb6f9ef78>
Where should I put the 'read' method. In private_messages_extensions.rb (the plugin source) it has:
module ClassMethods
# Ensures the passed user is either the sender or the recipient then returns the message.
# If the reader is the recipient and the message has yet not been read, it marks the read_at timestamp.
def read(id, reader)
message = find(id, :conditions => ["sender_id = ? OR recipient_id = ?", reader, reader])
if message.read_at.nil? && reader == message.recipient
message.read_at = Time.now
message.save!
end
message
end
end
module InstanceMethods
# Returns true or false value based on whether the a message has been read by it's recipient.
def read?
self.read_at.nil? ? false : true
end
What is the relationship between Class methods and Instance methods in relation to inserting directly into my own messages controller & model? I thought I inserted
def read(id, reader)
...
end
into the model, but the read? method in the instance methods section of the plugin code is confusing me and I continue to get the error on viewing a message.
Help appreciated!