On Oct 7, 8:31 pm, cult hero <[email protected]> wrote: > I hope this is enough context but basically, I have an articles table > that holds a bunch of language neutral information about a bunch of > articles (its slug, data published, source links, author links, etc) > and I have an article_contents_en (or es, or de or whatever) that has > the title, content and a few other fields in it that are language > specific. > > If I want to get a list in a particular language I select articles an > use eager_graph so that they're also selected with a single query. > > The Article class has this relation in it: > > many_to_one :en, > :class => :ArticleContentEn, > :key => :id, > :conditions => {:available => true}, > :graph_join_type => :inner, > :graph_select => [:title, :summary] > > This code: > > Article.order(:slug).eager_graph(:en).all > > Produces this SQL in the Rails log: > > (0.025353s) SELECT "articles"."id", "articles"."type_id", > "articles"."source_id", "articles"."slug", "articles"."available", > "articles"."restricted", "articles"."date_created", > "articles"."date_modified", "articles"."date_published", > "articles"."icon_path", "en"."title", "en"."summary" FROM "articles" > INNER JOIN "article_contents_en" AS "en" ON (("en"."article_id" = > "articles"."id") AND ("en"."available" IS TRUE)) ORDER BY "slug" > > That's close to what I want, but for the sake of the list a lot of > those fields don't matter so I added a select to grab a couple fields > that I actually care about. > > This code: > > Article.select(:id, :slug).order(:slug).eager_graph(:en).all > > Produces this SQL in the Rails log: > > (0.000475s) SELECT "id", "slug" FROM "articles" LIMIT 1 > (0.019753s) SELECT "articles"."id", "articles"."slug", "en"."title", > "en"."summary" FROM "articles" INNER JOIN "article_contents_en" AS > "en" ON (("en"."article_id" = "articles"."id") AND ("en"."available" > IS TRUE)) ORDER BY "slug" > > Any idea why that that first query is happening? I have no clue what > that first query is for but it doesn't appear to be doing anything or > be necessary.
It's a query to get the columns. There's two things you could do to get around it. First is to not select until after the eager_graph, by using set_graph_aliases: Article.order(:slug).eager_graph(:en).select(:id=>[:articles, :id], :slug=>[:articles, :slug], :title=>[:en, :title], :summary=>[:en, :summary]).all The second is just keep what you currently have, but move it into a constant: ArticleWithEn = Article.select(:id, :slug).order(:slug).eager_graph(:en) Then in your method, just call ArticleWithEn.all. This will still do the query once at startup, but will be faster every time you call the action. Jeremy -- You received this message because you are subscribed to the Google Groups "sequel-talk" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/sequel-talk?hl=en.
