Robert Wheaton
2005-Sep-21 02:31 UTC
''undefined method find'' (validation syntax problem)
Hello all, I''d be grateful if someone could help me with what is surely a very simple syntax question. In my schema, Writers have Portfolios; one of these Portfolios may be active per writer. There is a list of Portfolios with checkboxes; the user can check or uncheck an ''active'' box beside each Portfolio. Obviously I want some validation to ensure that no more than one Portfolio is active per writer at any one time. Here''s the action in the controller that is triggered when the box is checked (or unchecked): def toggle_active_portfolio portfolio = Portfolio.find(@params[:portfolio]) portfolio.active = !portfolio.use_for_lists? if portfolio.save etcetera end Here''s the validation (from the model): class Portfolio < ActiveRecord::Base belongs_to :writer def validate if self.find(:first, :conditions => [''writer_id = ? and active = 1'', self.writer_id]) errors.add(:active, "Another of your portfolios is already active.") end end For some reason I''m getting this exception: undefined method `find'' for #<Portfolio:0x24b208c> I assume I''ve done something very simple very wrong somewhere -- can anybody see it -- please? Thanks, Rob
Derek Haynes
2005-Sep-21 04:17 UTC
Re: ''undefined method find'' (validation syntax problem)
Rob, #find is a class method, not an instance method. You need to do something like: Portfolio.find(...) instead of self.find(...) Cheers, Derek On 9/20/05, Robert Wheaton <rwheaton-FhtRXb7CoQBt1OO0OYaSVA@public.gmane.org> wrote:> Hello all, > > I''d be grateful if someone could help me with what is surely a very > simple syntax question. In my schema, Writers have Portfolios; one of > these Portfolios may be active per writer. There is a list of > Portfolios with checkboxes; the user can check or uncheck an ''active'' > box beside each Portfolio. Obviously I want some validation to ensure > that no more than one Portfolio is active per writer at any one time. > > Here''s the action in the controller that is triggered when the box is > checked (or unchecked): > > > def toggle_active_portfolio > portfolio = Portfolio.find(@params[:portfolio]) > portfolio.active = !portfolio.use_for_lists? > > if portfolio.save > etcetera > end > > > Here''s the validation (from the model): > > > class Portfolio < ActiveRecord::Base > belongs_to :writer > > def validate > if self.find(:first, :conditions => [''writer_id = ? and active > 1'', self.writer_id]) > errors.add(:active, "Another of your portfolios is already > active.") > end > end > > > For some reason I''m getting this exception: > > undefined method `find'' for #<Portfolio:0x24b208c> > > I assume I''ve done something very simple very wrong somewhere -- can > anybody see it -- please? > > Thanks, > Rob > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Derek Haynes HighGroove Studios - http://www.highgroove.com Keeping it Simple. 404.593.4879
Mark Reginald James
2005-Sep-21 04:20 UTC
Re: ''undefined method find'' (validation syntax problem)
Robert Wheaton wrote:> if self.find(:first, :conditions => [''writer_id = ? and active = > ... > For some reason I''m getting this exception: > > undefined method `find'' for #<Portfolio:0x24b208c>Use either self.class.find or Portfolio.find here. -- We develop, watch us RoR, in numbers too big to ignore.
Robert Wheaton
2005-Sep-21 10:42 UTC
Re: ''undefined method find'' (validation syntax problem)
*Slaps forehead*. Thank you, Derek and Mark. Rob On 21-Sep-05, at 12:17 AM, Derek Haynes wrote:> Rob, > > #find is a class method, not an instance method. > > You need to do something like: > > Portfolio.find(...) instead of self.find(...) > > Cheers, > > Derek