I don't have any ideas. Could you give me any clues (like reference sites). Any help will be appreciated.
Model1: GROUP(id, name) Model2: USER_GROUP_CMB(id, user_id, group_id)
Expected SQL statement:
SELECT * FROM groups AS g LEFT OUTER JOIN user_group_cmbs AS cmb ON g.id = cmb.group_id WHERE cmb.user_id = 1
I tried to set up associations below but I dont know what to do after this.
class Group < ActiveRecord::Base has_many :user_group_cmb end class UserGroupCmb < ActiveRecord::Base has_many :group end
Rails Version: 3.1.1
I believe an includes
will use a LEFT OUTER JOIN
query if you do a condition on the table that the includes
association uses:
Group.includes(:user_group_cmb).where(user_group_cmbs: { user_id: 1 })
Rails 5+ allows you to do the following:
Group.left_outer_joins(:user_group_cmb)
http://api.rubyonrails.org/classes/ActiveRecord/QueryMethods.html#method-i-left_joins
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With