Hi, I have a def in my controller see below where i want to find the page object based on a parameter value and then use the page_id to then find all the contents that belong to that page in the same method. Controller def aboutus @page = Page.find_by_name(params[''About Us'']) @contents = Content.find(:all, :conditions => "page_id = #{@page.id}") end Page Model def self.find_by_name(pagename) find(:first, :conditions => "name = ''#{pagename}''") end I keep getting an error message "Called id for nil". Can anyone let me know what i''m doing wrong here. -- Posted via http://www.ruby-forum.com/.
John Butler wrote:> Hi, > > I have a def in my controller see below where i want to find the page > object based on a parameter value and then use the page_id to then find > all the contents that belong to that page in the same method. > > Controller > def aboutus > @page = Page.find_by_name(params[''About Us'']) > @contents = Content.find(:all, > :conditions => "page_id = #{@page.id}") > end> I keep getting an error message "Called id for nil". Can anyone let me > know what i''m doing wrong here.Sounds like @page is nil. Are you sure you params[''About Us''] contains a valid page name?> > Page Model > def self.find_by_name(pagename) > find(:first, > :conditions => "name = ''#{pagename}''") > end >Nice, but Rails already did this for you :-) ActiveRecord objects will create dynamic finders on the fly for you if you follow a certain convention: model.find_by_column(''value_to_search_for'') returns the first match, and model.find_all_by_column(''value_to_search_for'') returns all matches. Jeff softiesonrails.com -- Posted via http://www.ruby-forum.com/.
On 8/14/06, John Butler <johnnybutler7@gmail.com> wrote:> I have a def in my controller see below where i want to find the page > object based on a parameter value and then use the page_id to then find > all the contents that belong to that page in the same method. > > Controller > def aboutus > @page = Page.find_by_name(params[''About Us''])Do you really have a param named "About Us"? Are you sure you aren''t trying to pass "About Us" into that method? -- James
On 8/14/06, Jeff Cohen <cohen.jeff@gmail.com> wrote:> Nice, but Rails already did this for you :-) ActiveRecord objects will > create dynamic finders on the fly for you if you follow a certain > convention: > > model.find_by_column(''value_to_search_for'') > > returns the first match, and > > model.find_all_by_column(''value_to_search_for'') > > returns all matches.For all the gory details... http://blog.hasmanythrough.com/articles/2006/08/13/how-dynamic-finders-work -- Josh Susser http://blog.hasmanythrough.com
James Ludlow wrote:> On 8/14/06, John Butler <johnnybutler7@gmail.com> wrote: >> I have a def in my controller see below where i want to find the page >> object based on a parameter value and then use the page_id to then find >> all the contents that belong to that page in the same method. >> >> Controller >> def aboutus >> @page = Page.find_by_name(params[''About Us'']) > > Do you really have a param named "About Us"? Are you sure you aren''t > trying to pass "About Us" into that method? > > -- JamesThanks guys. Yes your right, the param is not called About Us i was just hard coding that value just to make sure the value i wanted was being passed to the model. There was nothing coming from the model thats why i was getting the nil error message. @page = Page.find(:first,:conditions => "name = ''About Us''") @contents = Content.find(:all, :conditions => "page_id = #{@page.id}", :order => ''pos, position_id'') -- Posted via http://www.ruby-forum.com/.