Hi, Say I have two models, User and Group whereby each User belongs to a Group, like so: class User < ActiveRecord::Base belongs_to :Group end class Group < ActiveRecord::Base has_many :Users end Whats "The Rails Way" TM to add a ''lookup'' validation to the User model such that Rails will check that the group exists in the DB on saving ? eg: u = User.new u[''group_id''] = group_id u.save I''ve looked at the ''validates_...'' helpers, but couldn''t find what I was looking for, ie, something like: validate_exists :group At the moment, the way forward seems to be something like: u = User.new u[''group_id''] = group_id if u.group.nil? # group_id not valid ... do something end u.save But I''d rather code something that fits with the Rails ''validates_'' framework Any comments welcome Nev
On Mon, 2005-07-18 at 13:26 +1000, Neville Burnell wrote:> Hi, > > Say I have two models, User and Group whereby each User belongs to a > Group, like so: > > class User < ActiveRecord::Base > belongs_to :Group > end > > class Group < ActiveRecord::Base > has_many :Users > end > > Whats "The Rails Way" TM to add a ''lookup'' validation to the User model > such that Rails will check that the group exists in the DB on saving ? > > eg: > > u = User.new > u[''group_id''] = group_id > u.save >I might go with some variation of (somewhat untested- but pretty much lifted straight out of everyone''s favorite new book): def add_user2group u = User.new u.save g= Group.find(group_id) g << u rescue flash[:notice] ="We''re sorry. There is no Group: #{group_id]}" redirect_to(:action => ''index'') end HTH, Howard
> I might go with some variation of (somewhat untested- but pretty much > lifted straight out of everyone''s favorite new book):> ...> rescue > flash[:notice] ="We''re sorry. There is no Group: #{group_id]}" > redirect_to(:action => ''index'')I always thought exceptions shouldn''t be used for routine error-handling; it seems a little flaky to intentionally cause an exception to trigger an error condition. - M
>> I always thought exceptions shouldn''t be used for routineerror-handling I agree ... In fact sometimes I think I''d prefer to see "find by id" not throw an exception at all ... Then I could use the same coding idiom for "find by id" as I do for "find first" and "find all", ie: if Group.find(id).nil? # do something end In the mean time, I use Group.find(:first, :conditions => [''id = ?'', id]).nil? But my question relates to avoiding coding the explicit ''find'' if possible -----Original Message----- From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org [mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Matt Powell Sent: Monday, 18 July 2005 2:07 PM To: lordkhaos-Wuw85uim5zDR7s880joybQ@public.gmane.org; rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org Subject: Re: [Rails] Validations and belongs_to: etc> I might go with some variation of (somewhat untested- but pretty much > lifted straight out of everyone''s favorite new book):> ...> rescue > flash[:notice] ="We''re sorry. There is no Group: #{group_id]}" > redirect_to(:action => ''index'')I always thought exceptions shouldn''t be used for routine error-handling; it seems a little flaky to intentionally cause an exception to trigger an error condition. - M _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
On Jul 18, 2005, at 12:20 AM, Neville Burnell wrote:>>> I always thought exceptions shouldn''t be used for routine > error-handling > > I agree ... > > In fact sometimes I think I''d prefer to see "find by id" not throw an > exception at all ... Then I could use the same coding idiom for "find > by > id" as I do for "find first" and "find all", ie: > > if Group.find(id).nil? > # do something > end > > In the mean time, I use Group.find(:first, :conditions => [''id = ?'', > id]).nil?Note that you can use Group.find_by_id(id) and it will return nil instead of throwing an exception. It''s a little bit easier on the typing. -Scott
>> Note that you can use Group.find_by_id(id)Thanks Scott, I had missed that form of ''find''
On Sun, 2005-07-17 at 23:57 -0400, Lord Khaos wrote:> I might go with some variation of (somewhat untested- but pretty much > lifted straight out of everyone''s favorite new book): > def add_user2group > u = User.new.. snip ..> g << uMy bad, this line should have been: g.users << u Sry, Howard