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 789241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T21:29:32+00:00 2026-05-14T21:29:32+00:00

I have tree tables books bookmarks users where there is a n to m

  • 0

I have tree tables

  • books
  • bookmarks
  • users

where there is a n to m relation from books to users trough bookmarks.

Im looking for a query, where I get all the books of a certain user including the bookmarks. If no bookmarks are there, there should be a null included…

my sql statement looks like:

SELECT * FROM `books` 
LEFT OUTER JOIN `bookmarks ` 
ON bookmarks.book_id = books.id 
AND bookmarks.user_id = ?

In rails I only know the :include statement, but how can I add the second bookmarks.user_id = ? statement in the ON section of this query? if I put it in the :conditions part, no null results would get returned!

Thanks!
Markus

  • 1 1 Answer
  • 3 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-14T21:29:33+00:00Added an answer on May 14, 2026 at 9:29 pm

    Add the following associations to your models:

    class Book < ActiveRecord::Base
      has_many :bookmarks
      has_many :users, :through => :bookmarks
    end
    
    # assuming bookmarks table has book_id and user_id columns.
    class Bookmark < ActiveRecord::Base
      belongs_to :book
      belongs_to :user
    end
    
    class User < ActiveRecord::Base
      has_many :bookmarks
      has_many :books, :through => :bookmarks
    end
    

    Now you can make following calls:

    u.books  # books for a user
    u.bookmarks   # bookmarks for a user
    
    # find the user and eager load books and bookmarks
    User.find(params[:id], :include => [:books, :bookmarks])
    
    
    b.users  # users for a book
    b.bookmarks   # bookmarks for a book
    
    # find the book and eager load users and bookmarks
    Book.find(params[:id], :include => [:users, :bookmarks])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my application, I have a tree with various object types (sources, tables, etc).
Hello All I am using infragsitics tree view control.I have one stored procedure that
I have tree tables, Customer, Invoice and InvoiceRow with the standard relations. These I
Let's say I have tree tables: [ news ] id title text date [
I have a set of tables that's actually a stubby tree. At the top,
I have tree tables: ID A ----------- 1 10 ID B ----------- 1 20
I have a basic file system tree that consists of two tables: folders =======================
I have a bunch of XSD's from https://github.com/XeroAPI/XeroAPI-Schemas/tree/master/v2.00 I need to do two things:
Let's say that I have tree tables User, Product and UserProduct. A user can
To simplify, I have tree tables: products , products-vs-orders , orders products fields :

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.