Given class User < AR::Base has_many :widgets end class Widget < AR:Base belongs_to :user end I think the following code makes sense: user = the_currently_logged_in_user widget = some_widget_object if widget.belongs_to? user widget.destroy end I attempted to run such code and was surprised when it didn''t work. Cheers, Gavin
On 4/14/05, Gavin Sinclair <gsinclair-81uBx+iSpXA0n/F98K4Iww@public.gmane.org> wrote:> if widget.belongs_to? user > widget.destroy > endThis is a good idea. I''m a little new to ruby, but this should work instead: if user.widgets.include? widget Though I think this will actually query the DB for all your user''s widgets and then instantiate all of those objects, so it''s probably a bit inefficient. Maybe something like this is better: if widget.user_id == user.id -- Urban Artography http://artography.ath.cx
> Maybe something like this is better: > > if widget.user_id == user.idYou can even do if widget.user == user since Active Record has been built to do object comparisons based on the id. -- David Heinemeier Hansson, http://www.basecamphq.com/ -- Web-based Project Management http://www.rubyonrails.org/ -- Web-application framework for Ruby http://www.loudthinking.com/ -- Broadcasting Brain