Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

has_many : << method doesn't handle :conditions

#718
class User < ActiveRecord::Base
  # Follow topics
  has_many :subscriptions
  has_many :bookmarks,     :through => :subscriptions, :source => :topic, :conditions => "subscriptions.notification = 'f'"
  has_many :notifications, :through => :subscriptions, :source => :topic, :conditions => "subscriptions.notification = 't'"
end
john.notifications << some_topic
INSERT INTO "subscriptions" ("topic_id", "notification", "user_id") VALUES(361323, NULL, 1638)

Expected sql would be:

INSERT INTO "subscriptions" ("topic_id", "notification", "user_id") VALUES(361323, "t", 1638)

( "t" for postgresql true boolean)

Reported by Jérôme · July 29th, 2008 @ 09:58 AM

State: wontfix
Milestone: 2.x
Assigned to: Tarmo Tänav Tarmo Tänav
Importance: Low

Activity

  1. Tarmo Tänav
    Tarmo Tänav
    • State changed from new to wontfix
    • Tag changed from 2.1, activerecord, has_many to 2.1, activerecord, has_many

    This will never happen, we can't start parsing arbitrary conditions, conditions are meant only as read-only restrictions on an association.

    You can however use :before_add callbacks to restrict what can be added to the collection.

    September 18th, 2008 @ 06:14 AM

  2. Jérôme
    Jérôme
    • Assigned user set to Tarmo Tänav

    Sure, but in my example, the :before_add hook can only handle the Topic, not the Subscription. :(

    September 22nd, 2008 @ 06:38 PM

  3. Jeff Kreeftmeijer
    Jeff Kreeftmeijer
    • Tag cleared.
    • Importance changed from to Low

    Automatic cleanup of spam.

    October 19th, 2010 @ 07:09 AM