Rails includes with conditions

前端 未结 7 1027
执笔经年
执笔经年 2020-12-08 03:33

Is is possible in Rails > 3.2 to add conditions to the join statement generated by the includes method?

Let\'s say I have two models, Person and Note. E

7条回答
  •  抹茶落季
    2020-12-08 04:31

    Same was discussed in Japanese stackoverflow. Quite hacky, but following seems to work, at least on rails 5.

    Person.eager_load(:notes).joins("AND notes.important = 't'")
    

    One important aspect is that by this way, you can write arbitrary join condition. Down side is that you cannot use placeholder so you need to be careful when using params as the join condition.

    https://ja.stackoverflow.com/q/22812/754

提交回复
热议问题