Nie do końca wiem jak zatytułować temat tego posta, ale postaram się wytłumaczyć na czym polega problem.
user.rb >>
[code] has_many :sent_messages,
:class_name => “Message”,
:foreign_key => ‘sender_id’,
:order => “messages.created_at DESC”,
:conditions => [“messages.sender_deleted = ? and messages.message_id is null”, false]
has_many :received_messages,
:class_name => “Message”,
:foreign_key => ‘recipient_id’,
:order => “messages.created_at DESC”,
:conditions => [“messages.recipient_deleted = ? and messages.message_id is null”, false][/code]
I generalnie tak; w kontrolerze wyświetlającym wiadomości prywatne mam @messages = current_user.received_messages - wyświetla, super. Ale jest jeden mankament - mianowicie; chciałbym, żeby użytkownik, który wyśle wiadomość widział ją również. Owszem, widzi ją - ale tylko dopiero, gdy odbiorca odpisze mu na wiadomość. Jak to rozwiązać?
message.rb >>
[code] belongs_to :sender,
class_name: “User”,
foreign_key: ‘sender_id’
belongs_to :recipient,
class_name: “User”,
foreign_key: ‘recipient_id’[/code]
Czy rozwiązaniem tego będzie has_and_belongs_to_many? Czy mam jeszcze jakieś inne, prostsze wyjścia?
@edit: Dodam jeszcze, że mam
belongs_to :message
has_many :messages
A więc wyświetlam wiadomości w formie (temat: wiadomość, wiadomość, wiadomość […])