Tony
Tony

Reputation: 10208

Rails has_many with nested condition

I have a model called site.

site has_many site_settings
site_settings belongs_to setting
setting has a boolean field called temporary

I want the relashionship between site and site_settings to return only the site_settings that are not associated to a temporary setting.

I have tried:

class Site < ActiveRecord::Base

  has_many :site_settings, :dependent => :destroy, 
    :conditions => {:setting => {:temporary => false} }

I also have tried some things with Proc.new, but with no success.

Any help?

Upvotes: 0

Views: 237

Answers (1)

Valery Kvon
Valery Kvon

Reputation: 4496

Don't forget to join/include Setting:

has_many :site_settings, :dependent => :destroy, :include => :setting, :conditions => {:setting => {:temporary => false}}

Upvotes: 1

Related Questions