0

I want to run a controller query and exclude the current (self) record. I can use this:

Model.where.not(slug: params[:id])

That works fine until you manage to pass the record id.

Is there a cleaner more direct way to do this?

UPDATE

I clarified my example. I want to search where the id OR slug columns are not equal to the params[:id]. This also would have to chain after any normal scope or where query I run.

Dan Tappin
  • 2,692
  • 3
  • 37
  • 77

2 Answers2

1

You can write this query

Model.where('slug <> :param_id or id <> :param_id', {param_id: params[:id]})
Vishal
  • 7,113
  • 6
  • 31
  • 61
0

You can use or:

Model.where.not(slug: params[:id]).or(Model.where.not(id: params[:id]))

Or you can rewrite your query:

Model.where('slug <> :param_id or id <> :param_id', {param_id: params[:id]})
Vishal
  • 7,113
  • 6
  • 31
  • 61
CAmador
  • 1,881
  • 1
  • 11
  • 19