I
I
IvanN7772014-12-16 09:27:59
Ruby on Rails
IvanN777, 2014-12-16 09:27:59

Scope conflict when joining one table, what to do?

In the model of tasks with users, there are 2 connections.

belongs_to :initiator, :class_name => 'User', :foreign_key => 'initiator_id'
belongs_to :designer, :class_name => 'User', :foreign_key => 'designer_id'

And two scope
scope :initiator_filter, ->(name){ joins(:initiator).where("users.name LIKE ?", "%#{name}%")  unless name.nil? }
 scope :designer_filter, ->(name){ joins(:designer).where("users.name LIKE ?", "%#{name}%")  unless name.nil? }

When working separately, both of them work.
Together a conflict arises and the latter works.
This is due to join on the same table, but I don't know how to fix it.(

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
vsuhachev, 2014-12-16
@IvanN777

joins('users initiators on initiators.id = initiator_id').where('initiators.name LIKE ...')

I
IvanN777, 2014-12-17
@IvanN777

The only join must be put in front

joins('LEFT JOIN users designers on designers.id = designer_id')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question