title: Rails 4 + ActiveRecord: How to exclude some records from the search?
body: <p>I have these tables:</p>
  
  <ol>
  <li><code>users</code> - a classic Devise table</li>
  <li><code>listings</code> - users create listings</li>
  <li><code>listings_exclude_users</code> - 2 columns: <code>user_id</code> and <code>listing_id</code></li>
  </ol>
  
  <p>user.rb</p>
  
  <pre><code>class User 
    has_many :listings
  end 
  </code></pre>
  
  <p>listing.rb</p>
  
  <pre><code>class Listing
    belongs_to :user
    has_many :listing_excluded_users
  end
  </code></pre>
  
  <p>listing_excluded_user.rb</p>
  
  <pre><code>class ListingExcludedUser
    belongs_to :user
    belongs_to :listing
  end
  </code></pre>
  
  <p>I label some users for which shouldn't be the respective listing visible and this information is saved in <code>listings_exclude_users</code>.</p>
  
  <p>Now I'd like to run a query that would fetch all the listings, but if is signed in the user who is in the table <code>listings_exclude_users</code> for the specific listing, then don't fetch this listing.</p>
  
  <p>I can do it in the view, but I'd prefer the ActiveRecord way.
  I tried</p>
  
  <pre><code>@listings = Listing.joins(:listing_excluded_users).where("listings.status='0' AND (listing_excluded_users.user_id != '#{current_user.id}')")
  </code></pre>
  
  <p>But this query doesn't return anything (empty <code>@listings</code>).</p>
  
  <p>Thank you in advance for advice.</p>