Question

I am chaining multiple where's in my model and it's look like that - user.watched_posts.commented_posts

where watched_posts and commented_posts is just method in the User model that calls where. Currently the results SQL have AND relationship between those where conditions,
SELECT * FROM users WHERE (--watched_posts-) AND (--commented_posts--)

How can I chain the methods but get and OR relationship?
SELECT * FROM users WHERE (--watched_posts-) OR(--commented_posts--)

I don't want to created method like 'watched_or_commented_posts' because it will be strange... And I have lot of other methods that will get chained and I need an OR relationship.

Your help will be appreciated, Yosy.

Was it helpful?

Solution

The ActiveRecord interface does not provide a wrapper for or. It is provided by Arel however, which is what ActiveRecord uses. You can access a model's arel table by using the arel_table class method.

Take a look at the OR section of the Arel github:

https://github.com/rails/arel

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top