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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:24:38+00:00 2026-06-14T20:24:38+00:00

I have a very simple model like this: class User < ActiveRecord::Base has_many :cookies

  • 0

I have a very simple model like this:

class User < ActiveRecord::Base
    has_many :cookies
    has_many :fortunes, :through => :cookies
end

class Cookie < ActiveRecord::Base
    belongs_to :user
    belongs_to :fortune
end

class Fortune < ActiveRecord::Base
    has_many :cookies
    has_many :users, :through => :cookies
end

For a given user, u, I can do

u.fortunes 

This will give me all the fortunes associated with this user via Cookies table. What I want to do is get all Fortunes not returned by u.fortunes.

I tried

Fortune.all(:limit => 5, :conditions => {:user => {:id._ne => u.id} })

but that doesn’t work :(. I am new to ActiveRecord.

Thanks

  • 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-06-14T20:24:39+00:00Added an answer on June 14, 2026 at 8:24 pm

    Or try this

    Fortune.includes(:cookies).limit(5).where([ 'cookies.user_id != ? OR cookies.user_id IS NULL', u.id ])
    

    Or with the syntax You use

    Fortune.all(:include => :cookies, :limit => 5, :conditions => [ 'cookies.user_id != ? OR cookies.user_id IS NULL', u.id ])
    

    The reason to not use include :users is to avoid one extra join.

    EDIT:
    The other suggestions are shorter, and I think also a little bit quicker when finding (no joins), I only wanted to show how to use associations.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have very simple data model like this: create table Company ( id int
I have Django model that looks like this: class Categories(models.Model): Model for storing the
I have a very simple user model. When I first created it, it had
I have this model class User include DataMapper::Resource include BCrypt property :id, Serial property
I have two models like this: class Store(models.Model): name = models.CharField(max_length=255) class Order(models.Model): store
This one has me utterly baffled... I have a very simple input model, with
I have a simple question. I have a model that looks like this: public
I have a very simple composite model made of two classes: Public Class ParentModelVM
I have a simple class that provides state codes like this: public class StateProvider
Supose we have a very simple model: Station has at least one Train Train

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.