Forum: Ruby on Rails validate callback balks when using AR.find method

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
4a9cd711ccb80f1ab3ce17450c2857e4?d=identicon&s=25 Greg Willits (-gw-)
on 2009-03-05 21:27
In an AR model (PrivilegedUser), I have several custom validations, so
am using validate method to call a sequence of other small methods which
perform specific tasks.

def validate
  do_this
  do_that
end

In do_this, I need to look up records from the same table/model I am in
to do some comparing of the current object being created/updated to
existing data.

def do_this
  results = PrivilegedUser.find(:all, :condition => .... )
  .....do some stuff.....
end

No matter what form I try that find method with (find, self.find,
PrivilegedUser.find), Rails balks that find is not a method of
PrivilegedUser.

If I substitute PrivilegedUser.find with some other class, it works just
fine. PrivilegedUser is a working AR subclass that I have been using for
months, and am now adding this new method, and I already have find being
used in other methods. So, there's something unique about trying to use
find in the validation callback somehow.

I'm obviously not understanding something. Can someone essplain to me?
Thx.

-- gw
5f94b9b346c2aa648a80bc259978e5bc?d=identicon&s=25 Colin Law (Guest)
on 2009-03-05 21:41
(Received via mailing list)
I do exactly what you describe with no problems.  Are you sure you have
not
just got a typo somewhere that your brain is refusing to see?

2009/3/5 Greg Willits <rails-mailing-list@andreas-s.net>
4a9cd711ccb80f1ab3ce17450c2857e4?d=identicon&s=25 Greg Willits (-gw-)
on 2009-03-05 21:51
Colin Law wrote:
> I do exactly what you describe with no problems.  Are you sure you have
> not just got a typo somewhere that your brain is refusing to see?

Always a possibility. I'll keep looking. But thanks for the verification
that it should work.

Other possible extenuating circumstances I realized might be worth
mentioning:

- this is a 1.2.6 legacy app
- PrivilegedUser is actually a subclass of an abstract class to
facilitate connecting to multiple databases w/in the same app. However,
this hasn't posed any problems anywhere else.

class CountyActiveRecord < ActiveRecord::Base
  @@county_domain = Thread.current[:county_domain]
  self.abstract_class = true
  establish_connection("#{RAILS_ENV}_#{@@county_domain}".to_sym)
end

class PrivilegedUser < CountyActiveRecord
  ..... etc etc.....
end

-- gw
This topic is locked and can not be replied to.