Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5950783
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:26:34+00:00 2026-05-22T17:26:34+00:00

I am teaching myself the framework and have been messing with Active Record. I’m

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T17:26:35+00:00Added an answer on May 22, 2026 at 5:26 pm
    class Chat
      belongs_to :user
      has_one :status, :through => :user 
      scope :readed, joins(:status).where(:status => {:is_read => true})
      scope :unreaded, joins(:status).where(:status => {:is_read => false})
    
      delegate :is_read, :to => :status
    end
    
    class Status
      belongs_to :user
      belongs_to :chat
    end
    
    class User
      has_many :chats
      has_many :statuses
    end
    
    @current_user.chats.readed #=> select all user chats where chat status is_read = true
    @current_user.chats.unreaded
    @current_user.chats.first.is_read
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Teaching myself some c# and my problem is that I have a class which
I've been teaching myself C++ and someone told me that C++ does not have
I'm still teaching myself how to bind and use observable collection. One problem that
I am teaching myself some Python and I have come across a problem which
I'm teaching myself VB.Net. Here is a problem I have recently come across. Say
I've been teaching myself cpp sporadically from 'accelerated C++' and recently I noticed that
I have been teaching myself programming for couple of years, and I was sure
I've been teaching myself C for a few months when I have time, and
I've been teaching myself Objective C recently, and have noticed the following pattern used
I'm teaching myself CIL and have been doing ok so far (just really started

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.