So I have a question that I haven''t been able to find the answer to. Let''s say I have an api method that :returns an ActiveRecord::Base object. In that class (say, Cat < ActiveRecord::Base) I have a method that finds a cat given by an id and returns that record in the database. so: api_method :findcat :expects => [{:catid}] :returns => [Cat] is the api call. The controller calls get_cat in Cat < ActiveRecord::Base, which does self.find(cat_id). Now, if there is no Cat with id cat_id, the result will be nil, according to the documentation for ActiveRecord''s find. Will things go boom if someone calls the api method with an non-existant id? Or will it return a Cat object that just happens to be nil? I''m thinking the latter but really aren''t sure. -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Don''t over-engineer your models in Rails. You already get what you''re looking for automatically. Given this class (entire contents) class Cat < ActiveRecord::Base end You can do the following: @cat = Cat.find(1) Will throw exception if you don''t pass in a valid id @cat = Cat.find_by_id(1) returns nil There''s no need for your own special API. This is why ActiveRecord exists - to keep you from having to write these methods yourself. #find and its variants are all public class methods. No need to wrap them :) Hope that helps. On Nov 30, 2007 1:04 PM, Erin Kiser <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > So I have a question that I haven''t been able to find the answer to. > > Let''s say I have an api method that :returns an ActiveRecord::Base > object. In that class (say, Cat < ActiveRecord::Base) I have a method > that finds a cat given by an id and returns that record in the database. > > so: > > api_method :findcat > :expects => [{:catid}] > :returns => [Cat] > is the api call. The controller calls get_cat in Cat < > ActiveRecord::Base, which does self.find(cat_id). > > Now, if there is no Cat with id cat_id, the result will be nil, > according to the documentation for ActiveRecord''s find. > > Will things go boom if someone calls the api method with an non-existant > id? Or will it return a Cat object that just happens to be nil? > > I''m thinking the latter but really aren''t sure. > -- > 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
This is really an oversimplification of what I want, but I do that because what I care about is retrieving an object. I find that for me when I am learning a new language, the simplest examples are the best to learn from. Thanks for your answer. -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---