-
Notifications
You must be signed in to change notification settings - Fork 12
Incompatible structure on seemingly compatible relations #1
Comments
Can you try the master version? 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 |
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'. |
Sorry, it's not relative(in where-or gem, we should always say |
https://github.com/Eric-Guo/where-or/blob/master/lib/where-or.rb#L88 Also needs the explicit ActiveRecord::Relation |
Yes, I just testing, kindly trying.. again.. I'm also testing, there is no testing suite in this gem 😄 |
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. |
I'm working one this now, please wait, seems much harder problem than just copy from master.. |
I will be patient. |
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
|
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 gem 'where-or', github: 'Eric-Guo/where-or', branch: 'no_structurally_compatible_check' if it work, means just need loose the checking condition at |
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" = ?) |
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. |
So, what I put in the gist is what we're using in production, fwiw |
Could probably close this non-issue 😄 |
I was pretty eager to utilize this gem, but have run into a snag.
So User has_many wikis that they own, and has_many collaborated wikis through collaborator. Wiki's has many users through collaborator. When I try:
I get an error from user.collaborated_wikis, which should return the relation of wikis related to this user. user.wikis works the same, by providing the wikis owned by user through collaborators. If I take out the collaborated_wikis call, even though scope.public_wikis and user.wikis are of different classes (ActiveRecord_Relation vs ActiveRecord_Associations_CollectionProxy, respectively) they play well together. If you look at the class of user.collaborated_wikis, it is that of ActiveRecord_Associations_CollectionProxy. Going into the rails console, I can see that the result of user.collaborated_wikis is a list of all the wikis that the user collaborates on, so I know the HMT relation works, I just can't figure out why the gem is saying that the structures are incompatible. Any ideas?
The text was updated successfully, but these errors were encountered: