Rails ActiveRecord where or clause
One way is to revert to raw sql...
YourModel.where("categories.id IN ? OR category_relationships.category_id IN ?", category_ids, category_ids)
Or & and in Rails ActiveRecord where clause
I may be wrong, but I don't think you could form that query using the Arel-based where function; you'd need to form up the database query string yourself.
Assuming you're using SQLite or Postgres:
Route.where("a = true and b = true and ((c > 0 and c < 1) or d = 1)").all
I haven't tested this code, but I suspect that might do the job for you. Note this is less 'portable' code; if you change the database you're using the query may break.
includes with where clause in ActiveRecord
You want to select where groups.shop_id is 1 or where there is no groups.shop_id
You can do that with a test for nil
Organisation.where(name: 'my organisation without groups').includes(:groups).where(groups: { shop_id: [nil, 1] })
Rails active record where OR clause with grandparent property
I think I finally got it:
scope.joins(story: :user).where("(stories.published = ? AND chapters.published = ?) OR stories.user_id = ?", true, true, @user.id)
Was hoping for a more non SQL statement way, but oh well, guess this will do unless someone know's the non-sql way.
How to control bracket position on ActiveRecord query with `OR` clause
You can get something similar, just without the main parentheses wrapping the where clause conditions:
User.where('foo IS NULL')
.or(User.where('10 > 20'))
.merge(User.where(bar: nil).or(User.where('10 > 1')))
# SELECT "users".*
# FROM "users"
# WHERE ((foo IS NULL) OR (10 > 20)) AND ("users"."bar" IS NULL OR (10 > 1))
Consider foo
equals 1
and bar
equals 2
.
Active Record .includes with where clause
This is how includes is intended to work. When you add a where clause it applies to the entire query and not just loading the associated records.
One way of doing this is by flipping the query backwards:
columns = Ticket.eager_load(:columns)
.where(sprint_id: 10, columns: { board_id: 1 })
.map(&:column)
.uniq
Rails 5: ActiveRecord OR query
The ability to chain or
clause along with where
clause in ActiveRecord
query will be available in Rails 5. See the related discussion and the pull request.
So, you will be able to do the following things in Rails 5:
To get a post
with id
1 or 2:
Post.where('id = 1').or(Post.where('id = 2'))
Some other examples:
(A && B) || C:
Post.where(a).where(b).or(Post.where(c))
(A || B) && C:
Post.where(a).or(Post.where(b)).where(c)
Rails where condition and order
So it looks like given a user ID, you want a list of companies sorted by the created_at
date of the bookmarks that join Users and Companies.
Am I correct in assuming that Users and Companies share a many-to-many relationship through Bookmarks?
If that's the case, the following "join" could work for you!
Company.joins(:bookmarks).where('bookmarks.user_id = ?', 7).order('bookmarks.created_at')
Of course, in your code, this could be generalized to grab companies for any user!
user_id = 42
companies = Company.joins(:bookmarks).where('bookmarks.user_id = ?', user_id).order('bookmarks.created_at')
ActiveRecord "joins" reference
Related Topics
How to Do Forward Fill as a Pl/Pgsql Function
Months Between Two Dates in SQL Server with Starting and End Date of Each of Them in SQL Server
Using Oracle Combine Three Tables to One with Pivot
Create a SQL View Based Converting Ranges into Rows
Ora-00600 When Running Alter Command
Cross Table Dependency/Constraint in SQL Database
Rolling Sum Previous 3 Months SQL Server
Varchar Requires a Length When Rendered on MySQL
SQL Server:Find Duplicates in a Table Based on Values in a Single Column
Oracle (11.2.0.1):How to Identify the Row Which Is Currently Updated by the Update Statement
SQL Server Normalization Tactic: Varchar VS Int Identity
Modify(Replace) Xml for Conditions
Choose As400 Query Records Directly from Excel
Query a Table and a Column Name Stored in a Table