Philip Hallstrom wrote:
> On Jan 12, 2010, at 9:46 AM, Steve Castaneda wrote:
> 
>> I'm assuming my controller would look something like this for Widgets:
>> --
>>
>> I'm assuming that it has to go in some kind of condition.  Am I  
>> supposed
>> to run the owners find through some sort of block to validate for each
>> widget I've found in @widgets?
> 
> 
> 
> @owners = Owner.all(:conditions => 'widget_id IS NULL')

No, you've got it backwards.  Widget belongs_to :owner, so that won't 
work.

The proper SQL query would be
SELECT * from owners o left join widgets w on (w.owner_id = o.id)
WHERE w.id is null

Deriving an AR find from that is left as an exercise. :)

> 
> Would do it.  Assuming that it's correct to say that if widget_id is
> null they have no widget -- this wouldn't catch instances where say an
> owner *had* widget, the widget was removed and the owners widget_id
> wasn't set back to null.

Nope.

> 
> @owners = Owner.all.reject{|o| o.widget.nil? }
> 
> Would also do it.  This has the drawback that the DB is going to
> return *all* the owners and then filter them in ruby.  Won't be as
> speedy as the first option.

Yup.  It will work, but it's completely inappropriate to do this sort of 
query in the app.

> 
> You probably want to add an :order option to whichever one you use to
> get a consistent output.
> 
> -philip

Best,
-- 
Marnen Laibow-Koser
http://www.marnen.org
[email protected]
-- 
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 [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.


Reply via email to