I am teaching myself the framework and have been messing with Active Record. I'm stuck with a problem that is easy to solve with raw SQL, and I'm wondering the best way to port it to Active Record ...
I have the following database schema ...
CREATE TABLE chats ( id int(11) DEFAULT NULL, user_id int(11) DEFAULT NULL, more longtext ) ENGINE=MyISAM DEFAULT CHARSET=latin1; CREATE TABLE statuses ( chat_id int(11) DEFAULT NULL, user_id int(11) DEFAULT NULL, is_read tinyint(1) DEFAULT NULL ) ENGINE=MyISAM DEFAULT CHARSET=latin1; CREATE TABLE users ( id int(11) DEFAULT NULL, name varchar(255) DEFAULT NULL, mail varchar(255) DEFAULT NULL ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
And I want to do the following in Active Record where the 1 in the join is the current user ID ...
SELECT c.*, s.is_read FROM chats c LEFT JOIN statuses s ON c.id = s.chat_id AND 1 = s.user_id
How do I set up this relationship so that I can have a simple 'is read' variable in my chat object? I know I can do this by performing various logic on the chat and status models I have but that seems far messier?