Coder Social home page Coder Social logo

Comments (14)

Eric-Guo avatar Eric-Guo commented on July 1, 2024

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.

craysiii avatar craysiii commented on July 1, 2024

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.

Eric-Guo avatar Eric-Guo commented on July 1, 2024

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.

craysiii avatar craysiii commented on July 1, 2024

https://github.com/Eric-Guo/where-or/blob/master/lib/where-or.rb#L88 Also needs the explicit ActiveRecord::Relation

from where-or.

Eric-Guo avatar Eric-Guo commented on July 1, 2024

Yes, I just testing, kindly trying.. again.. I'm also testing, there is no testing suite in this gem 😄

from where-or.

craysiii avatar craysiii commented on July 1, 2024

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.

Eric-Guo avatar Eric-Guo commented on July 1, 2024

I'm working one this now, please wait, seems much harder problem than just copy from master..

from where-or.

craysiii avatar craysiii commented on July 1, 2024

I will be patient.

from where-or.

craysiii avatar craysiii commented on July 1, 2024

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.

Eric-Guo avatar Eric-Guo commented on July 1, 2024

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.

craysiii avatar craysiii commented on July 1, 2024

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.

craysiii avatar craysiii commented on July 1, 2024

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.

bf4 avatar bf4 commented on July 1, 2024

So, what I put in the gist is what we're using in production, fwiw

from where-or.

craysiii avatar craysiii commented on July 1, 2024

Could probably close this non-issue 😄

from where-or.

Related Issues (10)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.