Question: I have a method which returns an ActiveRecord\_Relation and I would like to concat a `where`clause if the id received in `params` is not null. Something like this

```
query.where(item_id: params[:item_id]) # only if params[:item_id] is not null.

query # only if params[:item_id] is null.

```

It would be perfect a query like the first one:

```
query.where(item_id: params[:item_id])

```

in which, a nil value invalidates the whole where instead of searching an item\_id with nil id.

Of course I can do this using a conditional. But I would like to know if there is a rails way to do this. A one-liner or something

Thanks!

Answer: 
I hope that understood correctly what you want

Books with library\_id present in library list

```
//book[@library_id = //library/@id]

```

Books with incorrect library\_id

```
//book[not(@library_id = //library/@id)]

```
Why not use XSD's key/keyref constraints instead of ID/IDREF? These are much more flexible: they can be scoped to any element rather than the whole document; you can have different kinds of IDs within a document and the refs must match up to the same kind; the values can be composite (e.g. first name + last name); the values can be of any data type.