简体   繁体   中英

join twice on the same table rails?

I want to join on the same table twice to get username and username_to

@array = UsersWalletsBalancesFrozen.
joins("INNER JOIN userds userdsidto ON transactions.user_id_to=userdsidto.id").
joins("INNER JOIN userds userdsid   ON transactions.user_id = userdsid.id").select("*")

This get me the last join only works i think it's overrides is there any solution to that

Just as a quick solution... Have you tried to put it in the same method call:

@array = UsersWalletsBalancesFrozen.joins("INNER JOIN userds AS user_to userdsidto ON transactions.user_id_to=user_to.id INNER JOIN userds userdsid AS user_from   ON transactions.user_id = user_from.id").select("transactions.*, user_to.username, user_from.username")

?

PS: select('*') will be called by default. No need to explicitly specify it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM