Hello, I am trying to write some code to determine if a user is a member of a club using the has_many_and_belongs_to_many association, as follows: if @user.clubs.find(@club.id) ... end The problem I am running into is that the find throws an exception if it is not found. Is there simple way to do this without wrapping it in exception handlers? Thanks, Tom
On 07/07/2005, at 10:29 PM, Tom Davies wrote:> if @user.clubs.find(@club.id) > ... > end > > The problem I am running into is that the find throws an exception if > it is not found. Is there simple way to do this without wrapping it > in exception handlers?if (@user.clubs.find(@club.id) rescue nil) ... end Pete Yandell
Thanks Pete... that is definitely better. I am new to Ruby and Rails, but I am used to programming in such a way that Exceptions should not dictate the flow of your execution path. So, if the find method could not find something, I would have expected it to return nil, as it is a perfectly normal scenario to not find something. Is the "Ruby" way of doing things to throw Exceptions in scenarios such as this? Thanks again. Tom On 7/7/05, Pete Yandell <pete-AylieETRJAdBDgjK7y7TUQ@public.gmane.org> wrote:> > On 07/07/2005, at 10:29 PM, Tom Davies wrote: > > > if @user.clubs.find(@club.id) > > ... > > end > > > > The problem I am running into is that the find throws an exception if > > it is not found. Is there simple way to do this without wrapping it > > in exception handlers? > > if (@user.clubs.find(@club.id) rescue nil) > ... > end > > > Pete Yandell >
> I am new to Ruby and Rails, but I am used to programming in such a way > that Exceptions should not dictate the flow of your execution path. > So, if the find method could not find something, I would have expected > it to return nil, as it is a perfectly normal scenario to not find > something. Is the "Ruby" way of doing things to throw Exceptions in > scenarios such as this?One thing I like is my show action can have Widget.find(params[:find]) and have rescue_action_in_public return a 404. Course I wouldn''t have minded a Model.find!() for that (similar to save and save!). It would be very easy to do this yourself too. # use the actual args from ActiveRecord::Base#find module ActiveRecord class Base def find_without_exception(*args) find(*args) rescue nil end alias_method :find_with_exception, :find alias_method :find, :find_without_exception end end -- rick http://techno-weenie.net
In any language which supports exceptions, they are used to indicate exceptional cases. If record cannot be found, I''d rather have a nicely isolated exception handler that spread if nil? conditions all over my source code. ROR follows this rules: If you search a record by id, it will throw an exception if record is not found; if you search by some other criteria, it will return nil. Kent. On 7/7/05, Tom Davies <atomgiant-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Thanks Pete... that is definitely better. > > I am new to Ruby and Rails, but I am used to programming in such a way > that Exceptions should not dictate the flow of your execution path. > So, if the find method could not find something, I would have expected > it to return nil, as it is a perfectly normal scenario to not find > something. Is the "Ruby" way of doing things to throw Exceptions in > scenarios such as this? > > Thanks again. > Tom > > On 7/7/05, Pete Yandell <pete-AylieETRJAdBDgjK7y7TUQ@public.gmane.org> wrote: > > > > On 07/07/2005, at 10:29 PM, Tom Davies wrote: > > > > > if @user.clubs.find(@club.id) > > > ... > > > end > > > > > > The problem I am running into is that the find throws an exception if > > > it is not found. Is there simple way to do this without wrapping it > > > in exception handlers? > > > > if (@user.clubs.find(@club.id) rescue nil) > > ... > > end > > > > > > Pete Yandell > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
why not just use ActiveRecord::Base.exists?(id) instead of redefining find On 7/7/05, Kent Sibilev <ksruby-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> In any language which supports exceptions, they are used to indicate > exceptional cases. If record cannot be found, I''d rather have a nicely > isolated exception handler that spread if nil? conditions all over my > source code. > ROR follows this rules: If you search a record by id, it will throw an > exception if record is not found; if you search by some other > criteria, it will return nil. > > Kent. > > On 7/7/05, Tom Davies <atomgiant-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > Thanks Pete... that is definitely better. > > > > I am new to Ruby and Rails, but I am used to programming in such a way > > that Exceptions should not dictate the flow of your execution path. > > So, if the find method could not find something, I would have expected > > it to return nil, as it is a perfectly normal scenario to not find > > something. Is the "Ruby" way of doing things to throw Exceptions in > > scenarios such as this? > > > > Thanks again. > > Tom > > > > On 7/7/05, Pete Yandell <pete-AylieETRJAdBDgjK7y7TUQ@public.gmane.org> wrote: > > > > > > On 07/07/2005, at 10:29 PM, Tom Davies wrote: > > > > > > > if @user.clubs.find(@club.id) > > > > ... > > > > end > > > > > > > > The problem I am running into is that the find throws an exception if > > > > it is not found. Is there simple way to do this without wrapping it > > > > in exception handlers? > > > > > > if (@user.clubs.find(@club.id) rescue nil) > > > ... > > > end > > > > > > > > > Pete Yandell > > > > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
I am not positive, but I don''t think the exists? method will work on a has_many_belongs_to_many association. But I just had a look at the documentation again (http://api.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.html#M000429) and I just saw the association_ids method... which should do the trick (I will have to test it later though): @user.club_ids.include?(id) Thanks, Tom On 7/7/05, Francisco Hernandez <lagcisco-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> why not just use ActiveRecord::Base.exists?(id) instead of redefining find > > On 7/7/05, Kent Sibilev <ksruby-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > In any language which supports exceptions, they are used to indicate > > exceptional cases. If record cannot be found, I''d rather have a nicely > > isolated exception handler that spread if nil? conditions all over my > > source code. > > ROR follows this rules: If you search a record by id, it will throw an > > exception if record is not found; if you search by some other > > criteria, it will return nil. > > > > Kent. > > > > On 7/7/05, Tom Davies <atomgiant-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Thanks Pete... that is definitely better. > > > > > > I am new to Ruby and Rails, but I am used to programming in such a way > > > that Exceptions should not dictate the flow of your execution path. > > > So, if the find method could not find something, I would have expected > > > it to return nil, as it is a perfectly normal scenario to not find > > > something. Is the "Ruby" way of doing things to throw Exceptions in > > > scenarios such as this? > > > > > > Thanks again. > > > Tom > > > > > > On 7/7/05, Pete Yandell <pete-AylieETRJAdBDgjK7y7TUQ@public.gmane.org> wrote: > > > > > > > > On 07/07/2005, at 10:29 PM, Tom Davies wrote: > > > > > > > > > if @user.clubs.find(@club.id) > > > > > ... > > > > > end > > > > > > > > > > The problem I am running into is that the find throws an exception if > > > > > it is not found. Is there simple way to do this without wrapping it > > > > > in exception handlers? > > > > > > > > if (@user.clubs.find(@club.id) rescue nil) > > > > ... > > > > end > > > > > > > > > > > > Pete Yandell > > > > > > > _______________________________________________ > > > Rails mailing list > > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >