다음 SQL을 레일즈 쿼리에서 작동 시키려고합니다. Arel : 레일스 용 Arel 코드로 SQL 문을 변환하는 데 도움이 필요합니다.
쿼리
:select addr.*
from addresses addr
join users u
on addr.addressable_type = 'User'
and addr.addressable_id = u.id
join customers c
on c.id = u.actable_id
and u.actable_type = 'Customer'
where c.account_id = 1
and c.site_contact = 't'
이 내 레일 코드 :
# Inside my account.rb model
def site_addresses
a = Address.arel_table #Arel::Table.new(:addresses)
u = User.arel_table #Arel::Table.new(:users)
c = Customer.arel_table #Arel::Table.new(:customers)
# trying to debug/test by rendering the sql. Eventually, I want
# to return a 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 to debug, I'll remove this later
end
end
내가 좋아하는 오류를 받고 있어요 "알 수없는 클래스 : Arel :: 테이블". 임 시도 SQL 코드가 유효하기 때문에 제대로
어딘가 먼저 테이블을 나타내는 객체를 생성해야합니까? Like : address = Arel :: Table.new (: address)는 site_addresses 메소드에 있습니까? – bkunzi01