I have two classes with a one-to-many relationship, and want to find
all A records that don''t have any B records. I can''t seem to
make a
B.find(:all, :conditions => "blah") work because the foreign key is
in
A. Any help would be appreciated...
This is essentially what I have:
class A < ActiveRecord::Base
has_many :B
end
class B < ActiveRecord::Base
belongs_to :A
end
Am I going to have to do a :join to make this work with one sql
statement? (Just to move on I wrote the following, but I know it is
the wrong way to do it, just including so you can see the result I
want to achieve.. )
def self.no_B
ret = Array.new
self.find(:all, :include => :B ).each do |a|
ret << a if a.B.empty?
end
ret
end
--
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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
I would use a named_scope in this instance, assuming the relationship fields are completely normal. So the field that u want to check is the association id is nil. an example for that would be like this: named_scope :unassociated, :conditions => "foreignkey_id IS NULL" hope that works.. -- Posted via http://www.ruby-forum.com/. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
That is finding all B records that have no parent. I want to find all A records
that have no child...
I''m thinking that the best/fastest way to do this would to just use
counters.
You wrote:
I would use a named_scope in this instance, assuming the relationship
fields are completely normal.
So the field that u want to check is the association id is nil.
an example for that would be like this:
named_scope :unassociated, :conditions => "foreignkey_id IS NULL"
hope that works..
--
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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.