On Apr 7, 2005 9:00 AM, Rick Olson
<technoweenie-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> On Apr 6, 2005 3:57 PM, Nicholas Henry
<nicholas.henry-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
> > I would like to query a model like this:
> >
> > organizations has_one content_objects has_many tags
> >
> > with a condition like this:
> >
> > tags.tag = "live"
> >
> > I know how to do this in SQL:
> >
> > SELECT * FROM organizations o JOIN content_objects co ON (o.id >
> co.object_id) JOIN tags t on (co.id = t.content_object_id) where tag >
> "live"
> >
> > I am reasonably new with ruby/rails, but do understand using
> > associations for simple queries, but not quite sure how to use the AR
> > for more complex queries - do I just use the find_by_sql method? How
> > do you traverse models using AR?
> >
>
> find_by_sql would be the most efficient way.
Probably. You want to find all organisations with content with a
given tag? Whatever solution you choose make sure you encapsulate it.
class Organisation < ActiveRecord::Base
def find_all_with_tag(t)
# Some logic here
end
end
That way when you discover some magical optimisation you''re only
changing one piece of code.> --
> rick
> http://techno-weenie.net
> _______________________________________________
> Rails mailing list
> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
--
Cheers
Koz