I'm trying to clean up a very long ActiveRecord
query. The one I have is working, though it hurts to look at it. Here's what is happening.
1) User
has_many Simulations
through UserSimulations
(and vice versa).
2) User
has_many Groups
through UserGroups
(and vice versa).
3) Group
has_many Simulations
through SimulationGroups
(and vice versa).
What ends up happening here is that a user can be associated to a Simulation
in two ways, either directly through the has_many to has_many relationship, or indirectly through a Group
that the user belongs to.
I've been able to gather all the Simulations
a User has access to in a single query, and it looks like this. I have access to the current_user
object where the query needs to be called.
# Define Queries
user_sim_join = "LEFT JOIN user_simulations ON user_simulations.simulation_id = simulations.id"
user_grp_join = "LEFT JOIN user_groups ON user_groups.group_id = groups.id"
where_clause = ["user_groups.user_id = :user_id OR user_simulations.user_id = :user_id", { user_id: user.id }]
# Run Query
Simulation.joins(user_sim_join, :groups, user_grp_join).where(where_clause).group('simulations.id')
#=> Simulation Load (1.1ms) SELECT "simulations".* FROM "simulations"
INNER JOIN "simulation_groups" ON "simulation_groups"."simulation_id" = "simulations"."id"
INNER JOIN "groups" ON "groups"."id" = "simulation_groups"."group_id"
LEFT JOIN user_simulations ON user_simulations.simulation_id = simulations.id
LEFT JOIN user_groups ON user_groups.group_id = groups.id
WHERE (user_groups.user_id = 2 OR user_simulations.user_id = 2)
GROUP BY simulations.id
I'm happy that it's working but would like to clean it up as to be more concise (not 4 lines of code to build a single query).
Here are the models involved.
Simulation.rb
class Simulation < ApplicationRecord
# Associations
belongs_to :company
has_many :simulation_groups
has_many :user_simulations
has_many :objection_responses
has_many :groups, through: :simulation_groups
has_many :users, through: :user_simulations
has_many :reports, as: :reportable
has_many :objections, as: :objectionable
# Validations
end
Group.rb
class Group < ApplicationRecord
belongs_to :company
has_many :simulation_groups
has_many :simulations, through: :simulation_groups
has_many :user_groups
has_many :users, through: :user_groups
has_many :minigame_groups
has_many :minigames, through: :minigame_groups
has_many :reports, through: :users
# Validations
end
User.rb
class User < ApplicationRecord
# Associations
belongs_to :company
has_one :avatar
has_many :reports
has_many :events
has_many :training_sessions
has_many :user_groups
has_many :groups, through: :user_groups
has_many :objection_responses
has_many :user_simulations
has_many :simulations, through: :user_simulations
has_many :minigame_users
has_many :minigames, through: :minigame_users
# Validations
end
What about this?
scope :from_user, lambda{|user|
joins('left join user_simulations us ON (us.simulation_id = simulations.id)').
joins('left join group_simulations gs ON (gs.simulation_id = simulations.id)').
where("us.user_id = ? OR gs.group_id IN (?)",
user.id,
UserGroup.select("group_id").where(user_id: user.id))}
Simulation.from_user(user).to_sql:
"SELECT `simulations`.* FROM `simulations`
left join user_simulations us ON (us.simulation_id = simulations.id)
left join group_simulations gs ON (gs.simulation_id = simulations.id)
WHERE (us.user_id = 1 OR gs.group_id IN (
SELECT `user_groups`.`group_id` FROM `user_groups`
WHERE `user_groups`.`user_id` = 1))"
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.