I don`t want to use 'find_by_sql', but currently it is the only way to get ordered relation for pagination. It would be good to use 'last' and 'first' methods.

I have modelname.where(...).union(modelname.where(...)) how i can get something like modelname.where(...).union(modelname.where(...)).order('something')?

So essentially you’re trying to do something like this?

modelname.where(:name => “foo”).union(modelname.where(:name => “bar”))

Since it’s the same model, I don’t think there’s a reason to use a union in this case. Isn’t this really just an OR operator?

select * from modelname where name = “foo” or name = “bar”

Couldn’t you just use the or operator like so?

modelname.where(“name = ? or name = ?”, “foo”, “bar”).order(“something”)

Granted, your WHERE clause is probably more complex than that, but the principle should be the same.