Comments (14)
Can you try the master version? gem 'where-or', github: 'Eric-Guo/where-or'
I just blindly port from Rails master in structurally_compatible_for_or check. (no testing.)
unless structurally_compatible_for_or?(other)
raise ArgumentError, 'Relation passed to #or must be structurally compatible'
end
from where-or.
I installed the master version per your instructions. Now I am getting a different error.
uninitialized constant ActiveRecord::QueryMethods::Relation:
Extracted source (around line #100):
def new_where_clause
Relation::WhereClause.empty # Line 100
end
alias new_having_clause new_where_clause
end
This happens on any page, even one that doesn't have a call to 'where-or'.
from where-or.
Sorry, it's not relative(in where-or gem, we should always say ActiveRecord::Relation::WhereClause.empty
), maybe I port too much, just rollback the change, now OK?
from where-or.
https://github.com/Eric-Guo/where-or/blob/master/lib/where-or.rb#L88 Also needs the explicit ActiveRecord::Relation
from where-or.
Yes, I just testing, kindly trying.. again.. I'm also testing, there is no testing suite in this gem
from where-or.
I am happy to test with you, I realize that this is relatively new!
Now I am getting the first error: "Relation passed to #or must be structurally compatible."
I can provide more context for each relation if you need it.
Question: https://github.com/Eric-Guo/where-or/blob/master/lib/where-or.rb#L90 Is the comparison on this line supposed to be != ? Maybe it should be == ? I am not sure, just a guess.
from where-or.
I'm working one this now, please wait, seems much harder problem than just copy from master..
from where-or.
I will be patient.
from where-or.
More contextual information:
[2] pry(main)> u.wikis
Wiki Load (0.2ms) SELECT "wikis".* FROM "wikis" WHERE "wikis"."user_id" = ? [["user_id", 1]]
=> [#<Wiki:0x00000007999ba0
id: 1,
title: "Nobis et molestiae rerum qui.",
body:
"[\"Eligendi pariatur soluta aliquid id accusamus. Officia a quam illo velit quisquam non. Repudiandae ea consectetur voluptates ut expedita reiciendis. Accusamus ab autem. Saepe in dolores magnam rerum quam voluptatibus praesentium.\", \"Cum eligendi rerum earum optio natus dolores. Voluptatem accusantium aut quasi nemo molestiae. Ea esse consectetur eos. Sit dolorem nam aut possimus. Et suscipit temporibus.\", \"Odit quam fugit quasi sunt voluptatem nisi. Nihil ipsum veritatis omnis. Deleniti pariatur et quam non ipsum. Modi inventore magnam amet assumenda quo et. Voluptas voluptatem sed iure dolor doloribus qui.\"]",
private: false,
user_id: 1,
created_at: Thu, 10 Sep 2015 20:19:56 UTC +00:00,
updated_at: Thu, 10 Sep 2015 20:19:56 UTC +00:00,
permalink: "nobis-et-molestiae-rerum-qui">, ...]
[3] pry(main)> u.collaborated_wikis
Wiki Load (0.2ms) SELECT "wikis".* FROM "wikis" INNER JOIN "collaborators" ON "wikis"."id" = "collaborators"."wiki_id" WHERE "collaborators"."user_id" = ? [["user_id", 1]]
=> [#<Wiki:0x00000007c989c8
id: 65,
title: "some wiki title",
body: "some body here",
private: false,
user_id: 9,
created_at: Fri, 18 Sep 2015 20:37:55 UTC +00:00,
updated_at: Fri, 18 Sep 2015 20:37:55 UTC +00:00,
permalink: "some-wiki-title">]
[4] pry(main)> Wiki.public_wikis
Wiki Load (1.1ms) SELECT "wikis".* FROM "wikis" WHERE "wikis"."private" = ? [["private", "f"]]
=> [#<Wiki:0x00000002f85d98
id: 1,
title: "Nobis et molestiae rerum qui.",
body:
"[\"Eligendi pariatur soluta aliquid id accusamus. Officia a quam illo velit quisquam non. Repudiandae ea consectetur voluptates ut expedita reiciendis. Accusamus ab autem. Saepe in dolores magnam rerum quam voluptatibus praesentium.\", \"Cum eligendi rerum earum optio natus dolores. Voluptatem accusantium aut quasi nemo molestiae. Ea esse consectetur eos. Sit dolorem nam aut possimus. Et suscipit temporibus.\", \"Odit quam fugit quasi sunt voluptatem nisi. Nihil ipsum veritatis omnis. Deleniti pariatur et quam non ipsum. Modi inventore magnam amet assumenda quo et. Voluptas voluptatem sed iure dolor doloribus qui.\"]",
private: false,
user_id: 1,
created_at: Thu, 10 Sep 2015 20:19:56 UTC +00:00,
updated_at: Thu, 10 Sep 2015 20:19:56 UTC +00:00,
permalink: "nobis-et-molestiae-rerum-qui">, ...]
[5] pry(main)> u.wikis.class
=> Wiki::ActiveRecord_Associations_CollectionProxy
[6] pry(main)> u.collaborated_wikis.class
=> Wiki::ActiveRecord_Associations_CollectionProxy
[7] pry(main)> Wiki.public_wikis.class
=> Wiki::ActiveRecord_Relation
from where-or.
I plan to writing a testing case in Rails master (Rails 5) to reproduce such case, to see if it's same or work.
I just remove structurally_compatible_for_or
check, Kindly try that branch if work for your case.
gem 'where-or', github: 'Eric-Guo/where-or', branch: 'no_structurally_compatible_check'
if it work, means just need loose the checking condition at structurally_compatible_for_or
, otherwise means Rails 5 also need to handle that case usage, using has_many :collaborated_wikis, through: :collaborators, source: :wiki
in or
from where-or.
It does not work, seemingly because that relation (User.collaborated_wikis) has a JOIN statement.
SQLite3::SQLException: no such column: collaborators.user_id: SELECT "wikis".* FROM "wikis" WHERE (("wikis"."private" = ? OR "wikis"."user_id" = ?) OR "collaborators"."user_id" = ?)
from where-or.
This query gave me what I wanted:
wikis = Wiki.find_by_sql(
['SELECT "wikis".* FROM "wikis" WHERE ("wikis"."private" = ? OR "wikis"."user_id" = ?) UNION ALL SELECT "wikis".* FROM "wikis" INNER JOIN "collaborators" ON "wikis"."id" = "collaborators"."wiki_id" WHERE "collaborators"."user_id" = ?;', false, user.id, user.id])
So by using UNION ALL between the SQL generated by" wiki.public_wikis.or(user.wikis)" and "user.collaborated_wikis", I was able to get the result. I don't know if #or should be responsible for this complex logic. I don't necessarily think you should work on this anymore, if it's in line with Rails 5. Maybe this should be it's own method (ActiveRecord#union) so one could write the query like so:
wikis = Wiki.public_wikis.or(user.wikis).union(user.collaborated_wikis)
Thanks for looking at this and let me know if you'd like further testing.
from where-or.
So, what I put in the gist is what we're using in production, fwiw
from where-or.
Could probably close this non-issue
from where-or.
Related Issues (10)
- Gem Load Error is: nil is not a symbol
- Does it worked in Rails 5? HOT 3
- Any plans to include tests? HOT 1
- `Relation passed to #or must be structurally compatible` fix? HOT 2
- undefined method `predicates' for #<Array:0x000055fa923e6858> HOT 1
- chaining .or with .all clause HOT 1
- where-or breaks preloading of associations with conditions HOT 5
- Could you publish a new version of the gem? ie, 0.1.2 HOT 2
- Eager-loading unscoped association generates empty WHERE clause
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from where-or.