Unexpected behavior in Rails3.2 ActiveRecord finder methods and Globalize3 #185

faustoct opened this Issue Dec 27, 2012 · 5 comments


None yet
2 participants

I have complex Model query with globalized fields, containing:

  1. scalar-fields: select("field1, field3, joined1.field1, count(1), ...")

  2. inner joins: joins(:book =>[{:article => :writer}, ... ])

  3. where: where("writer.name like ?", "%#{name}%")

  4. group-by: group("field1, field3, ... ")

  5. order-by: order("count(1) desc")

This query works but show me a lot of problems: n+1 queries, retrieve unrequested fields (globalize fields on join i18n), unaccepted clauses in select as count(1), always retrieve globalized fields, get only fully globalized fields and partial or some fields dont work ...

I tried many alternatives to solve these problems:

  1. use includes to eager-loading to solve n+1 problem. But I have problem to include some fields in select finder, and all translations fields are retrieved

  2. add :translations in joins finder method. But doesnt solve n+1 problem and retrieve all globalized fields

  3. i tried use with_translation(locale)

I think that Globalize3 + Rails3 works but in some cases like complex queries has a pitfall it. Because it havent deterministic behavior and control it with ActiveRecord finder methods.

My solution: Wrote query using ActiveRecord::Base.connection.select as ansi-sql. Added all joins that globalize would did for me. I dont using finder methods, ...

faustoct closed this Dec 27, 2012

faustoct reopened this Dec 27, 2012


parndt commented Dec 27, 2012

This doesn't give us anything to go on in order to help you resolve
this :( are you able to provide us with some actual code that shows

In my experience if you write the query well and don't try to access
any fields you haven't loaded then there are no n+1 problems.

This is example MyModel class example.

class MyModel < ActiveRecord::Base
  attr_accessible :channel_id, :foundation_year, :link_institution, :link_photo, :link_photo_logo, :link_wikipedia, :localization, :slogan, :sprite_position, :year_budget, :name, :complete_name
  attr_accessible :description, :slogan_translated 

  translates :name, :complete_name, :description, :slogan_translated

  belongs_to :channel
  has_many :courses
  has_many :lectures, :through => :courses

  class Translation
    attr_accessible :locale

  def self.occurs sentence
    return MyModel.select("courses.link_photo_mini, courses.number_lectures, lectures.id, lectures.number_lecture, lectures.views_youtube, teachers.name, count(1) as counter").joins(:translations, :courses => [{:lectures => :teacher}, :subjects ]).where("teachers.name like ?", "%#{sentence}%").group("courses.link_photo_mini, lectures.id, lectures.number_lecture, teachers.name").order("counter DESC")


Notes that self.occurs methods load instances of MyModel. In this case occurs n+1 problem and I if try add includes(:translations) or joins(:translations), the query fails in counter. And some cases all fields not belong MyModel/MyModel_Translation didn't load.
Make me sense load all attributes entity's in load full entity but not in scalar-query wheres I specify fields in select clause.

Notes that I dont specify globalized fields in my query but it loaded.

faustoct commented Jan 3, 2013

My post did help you?

faustoct commented Jan 9, 2013

Hi. If you could explain why this query works fine I appreciate your time.


Both cases and others get me unexpected result. I think that "includes" and "translations" field dont work fine.

This query always return MyModel fields and respectly Translation fields. But if remove "includes(:translations)" query fails



parndt commented Jun 27, 2013

I believe that this is related to a bug in Rails

parndt closed this Jun 27, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment