camertron / scuttle-rb

A library for transforming raw SQL statements into ActiveRecord/Arel queries. Ruby wrapper and tests for scuttle-java.
86 stars 2 forks source link

SQL negating a LIKE doesn't appear to be generating correct arel code #19

Open roscom opened 12 months ago

roscom commented 12 months ago
SELECT clients.*  FROM clients  
    INNER JOIN roles ON roles.roleable_type = 'Client' AND roles.roleable_id = clients.id  
    INNER JOIN participants ON participants.expired_on IS NULL AND participants.id = roles.participant_id  
    WHERE participants.expired_on IS NULL AND roles.expired_on IS NULL AND NOT ((participants.name_search LIKE '%tway%' OR participants.name_search LIKE '%legal%')) order by short_name;

produces the following arel code:

Client.select(Client.arel_table[Arel.star]).where(
  Participant.arel_table[:expired_on].eq(nil).and(
    Role.arel_table[:expired_on].eq(nil).and(
      Participant.arel_table[:name_search].or(Participant.arel_table[:name_search])
    )
  )
).joins(
  Client.arel_table.join(Role.arel_table).on(
    Role.arel_table[:roleable_type].eq('Client').and(
      Role.arel_table[:roleable_id].eq(Client.arel_table[:id])
    )
  ).join_sources
).joins(
  Client.arel_table.join(Participant.arel_table).on(
    Participant.arel_table[:expired_on].eq(nil).and(
      Participant.arel_table[:id].eq(Role.arel_table[:participant_id])
    )
  ).join_sources
).order(:short_name)

The match method is omitted from

Participant.arel_table[:name_search].or(Participant.arel_table[:name_search]

It should be

Participant.arel_table[:name_search].matches(%tway%).or(Participant.arel_table[:name_search].matches('%legal%')

I would appreciate it if you could take a look and let me know what I may have done incorrectly or if it's Scuttle