1

I thought I had this solved in a previous post but I'm still struggling with it. In my Rails app I'm trying to give user the ability to message each other within the app (Not through email). I'm using rails 3.2.8 and ruby 1.9.3p194

Message belongs_to :user

User has_many :messages

Here is my method for sending messages in my Message Model

  def self.send_message(from, recipient)
     recipient do |recipient|
       msg = self.clone
       msg.sent = false
       msg.user_id = recipient
       msg.save
     end
     self.update_attributes :user_id => from.id, :sent => true
   end

I'm trying to test this in the rails console by doing: Message.send_message(u2,u)

where u2 = User.find(1) & u = User.find(2)

I keep getting the error NoMethodError: undefined method `recipient' for #

What am I doing wrong? How can I properly send a user a message from another user.

I'm open to any strategy if someone has a better way.

DaveG
  • 1,203
  • 1
  • 25
  • 45
  • couple of question 1) if your doing `Message.send_message(u2,u)` so I presume `recepient` is `user` object then what is block doing over there 2) msg = self.clone msg.sent inside a class method will give to the class msg.sent msg.user_id and msg.save on class object i.e msg I feel it should respond to instance method of class not sure doing it right correct me if I'm wrong – Viren Nov 10 '12 at 07:27
  • @Viren I'm not sure I fully understand you questions and I'm fairly new at coding. I've been following the answer to this question http://stackoverflow.com/questions/5141564/model-users-message-in-rails-3 which hopefully clarifies a few things. – DaveG Nov 10 '12 at 08:19
  • 1
    you need to have structure like [this](http://pastie.org/5355281) Please ignore some `TYPO` error – Viren Nov 10 '12 at 10:42
  • Thanks Viren that code works. – DaveG Nov 10 '12 at 14:28

1 Answers1

1

I'm not sure this has a good answer. I ended up switching up my table a little and going with https://github.com/LTe/acts-as-messageable

DaveG
  • 1,203
  • 1
  • 25
  • 45