I have a page where I display a list of threads for a user whether the tread was started by them or if they were the recipient of one started.
Here is my model:
has_many :threads_as_starter, :class_name => 'MessageThread', :foreign_key => 'sender_id'
has_many :threads_as_recipient, :class_name => 'MessageThread', :foreign_key => 'recipient_id'
What I’d like to do is define a method that I can store in a in instance variable and loop through on my view page that displays threads of the current_user.
When I run: MessageThread.where( ‘sender_id OR recipient_id = ?’, 4 )
1.9.3p0 :045 > MessageThread.where( 'sender_id OR recipient_id = ?', 4 )
MessageThread Load (0.4ms) SELECT `message_threads`.* FROM `message_threads` WHERE (sender_id OR recipient_id = 4) ORDER BY message_threads.created_at DESC
=> [#<MessageThread id: 89, message_id: 219, sender_id: 4, recipient_id: 38, status: 0, created_at: "2012-02-15 12:26:17", updated_at: "2012-02-15 12:26:17">, #<MessageThread id: 88, message_id: 218, sender_id: 2, recipient_id: 4, status: 0, created_at: "2012-02-14 13:41:19", updated_at: "2012-02-14 13:41:19">, #<MessageThread id: 87, message_id: 210, sender_id: 1, recipient_id: 2, status: 0, created_at: "2012-02-14 13:31:12", updated_at: "2012-02-14 13:31:12">]
I’m confused as to why it’s showing me rows where the sender or recipient id isn’t equal to 4.
What query would return all results where the sender_id is 4 but also all the results where the recipient_id is 4? I need to give the signed in user to see a list of all their current threads. Ones that were started by them and ones that weren’t.
User A and B only have 1 thread in the message_threads table but either of them can have more threads but with different users but only 1 with each of those users. I use this message_thread table to reference conversations in my messages table where I use the acts_as_tree gem.
There must be a way I can group threads_as_starter and threads_as_recipient e.g. all_threads then call current_user.all_threads to return all.
Thanks in advance
Kind regards
should be
or probably