Dan Fitzpatrick
2007-Jan-07 02:58 UTC
ActiveRecord.find :include and :conditions(value or null)
How do you get left-joined data where a related record''s data is either 0 or nil (because no records are found) but not 1? I have the following data: Table a ----- id 1 2 3 Table b ----- id a_id x 10 1 1 11 1 1 12 2 0 13 2 0 14 2 1 (No record with a_id = 3) Ruby code: ActiveRecord::Base::pluralize_table_names = false Class A < ActiveRecord::Base has_many :b end Class B < ActiveRecord::Base belongs_to :a end # The following ActiveRecord.find will not return any results for A.id = 1 a = A.find(1, :include=>[:b], :conditions=>"(b.x = 0 or b.x is null)") #=> not found error (I want this to return a.b = empty array) a = A.find(2, :include=>[:b], :conditions=>"(b.x = 0 or b.x is null)") #=> One A record with two b records where a.b[0].x and a.b[1].x = 0 a = A.find(3, :include=>[:b], :conditions=>"(b.x = 0 or b.x is null)") #=> One A record with a.b = [] How do I do this find so that A''s with b.x = 0 or b = [] (no related records are found)? Thanks, Dan --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---