join - Arel: Need help converting SQL statement into Arel code for Rails -


trying following sql work in rails query.

the query:

select addr.*   addresses addr   join users u     on addr.addressable_type = 'user'    , addr.addressable_id = u.id   join customers c      on c.id = u.actable_id    , u.actable_type = 'customer'   c.account_id = 1    , c.site_contact = 't' 

this rails code:

# inside account.rb model def site_addresses     = address.arel_table #arel::table.new(:addresses)     u = user.arel_table #arel::table.new(:users)     c = customer.arel_table #arel::table.new(:customers)      # trying debug/test rendering sql. eventually, want     # return relation array of addresses.     sql = address.       joins(u).         on(a[:addressable_type].eq("user").and(a[:addressable_id].eq(u[:id]))).       joins(c).         on(c[:id].eq(u[:actable_id]).and(u[:actable_type].eq("customer"))).      where(c[:account_id].eq(self.id).and(c[:site_contact].eq(true))).to_sql      raise sql.to_yaml #trying debug, i'll remove later   end end 

i'm getting errors "unknown class: arel::table". im not using arel correctly because sql code valid (i can run on database fine)

try following:

a.join(u).on(a[:addressable_type].eq("user")... # using arel_table , "join" instead 

i based answer docs:

users.join(photos).on(users[:id].eq(photos[:user_id])) 

Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -