Rails 3 SQL query select with joins

前端 未结 2 1201
谎友^
谎友^ 2021-01-28 05:32

I have a problem with select fonction and joins.

here is my current query.

@search = Building.joins(\'INNER JOIN \"floors\" ON \"floors\".\"building_id\"         


        
相关标签:
2条回答
  • 2021-01-28 06:08

    You can do it like this... Hope its useful

    @search = Building.select("buildings.name, floors.number, spaces.number").joins("INNER JOIN floors ON floors.building_id = buildings.id INNER JOIN spaces ON spaces.floor_id = floors.id")
    
    0 讨论(0)
  • 2021-01-28 06:17

    Look at your code:

    Building.select('buildings.name, floors.number, spaces.number)...
    

    You don't select the building id, so Rails is a bit lost when comes the time to retrieve it.

    0 讨论(0)
提交回复
热议问题