Forum: Ruby on Rails does has_many: add a has_<associated_class>? 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.
unknown (Guest)
on 2006-05-24 01:51
(Received via mailing list)
Hi,

I have a Country class which has_many: regions. At the same time, the
corresponding countries table has a boolean column named has_regions.
It happens that when I try to access that attribute using
country_instance.has_regions? , I can see in the logs that Rails is
querying the database with a SELECT that's counting the regions in that
country. Obviously, that's not what I want, as I already have the
required info in the has_regions column, so there's no need to make a
database query.

Is this "synthetic" method documented in the Rails docs? I can't find
it in the has_many documentation...
http://api.rubyonrails.org/classes/ActiveRecord/As...
ClassMethods.html#M000530

How can I avoid that query? Is there any way apart from changing the
name of the has_regions column?

Thanks in advance,

xavi
This topic is locked and can not be replied to.