Giter Club home page Giter Club logo

eager_group's Issues

problem when FK is not an integer id

I have an association where I use a text string for my primary key.

      has_many :children, class_name: "ActsAsTaggableOn::Tag", foreign_key: :parent_name, primary_key: :name, dependent: :nullify
      define_eager_group :children_count, :children, :count, :*

when I do eager_group count, it is associating with the object's id instead of the association's PK (:name)

irb(main):016:0> tag = ActsAsTaggableOn::Tag.find_by(name: "Africa")                    
irb(main):017:0> tag.children.count
  ActsAsTaggableOn::Tag Count (0.5ms)  SELECT COUNT(*) FROM "aat_tags" WHERE "aat_tags"."parent_name" = $1  [["parent_name", "Africa"]]
=> 60                                                                                     
irb(main):018:0> tag.children_count
  ActsAsTaggableOn::Tag Count (1.2ms)  SELECT COUNT(*) AS "count_all", "aat_tags"."parent_name" AS "aat_tags_parent_name" FROM "aat_tags" WHERE "aat_tags"."parent_name" = $1 GROUP BY "aat_tags"."parent_name"  [["parent_name", "3018"]]
=> 0                                                                                      
irb(main):019:0> tag.id
=> 3018

distinct values?

Let's say I have Departments and Employees, and every employee has an employee_type "temporary", "permanent", "retired", I dunno.

I wanna fetch a list of Departments and for each one have an attribute with an array of distinct employee_types found in that Department's employees.

I could hack out the proper GROUP BY query to get these (although I haven't yet). Using postgres-specific functions maybe even get the multiple unique/distinct values in a single SQL result row, as a pg array or something. Although I don't know if I'd need to do that if I was just writing it out manual.

Is there any hope of getting eager_group flexible enough to let me write this and have it magically done and have the array assigned to a model attributes?

aggregate_function count return nil when no record on has many association

If I add post3 in data.rb

post1 = Post.create(title: "First post!")
post2 = Post.create(title: "Second post!")
post3 = Post.create(title: "Third post!")

post1.comments.create(status: 'created', rating: 4)
post1.comments.create(status: 'approved', rating: 5)
post1.comments.create(status: 'deleted', rating: 0)

post2.comments.create(status: 'approved', rating: 3)
post2.comments.create(status: 'approved', rating: 5)

eager_group_spec.rb

      it 'gets both Post#approved_comments_count and Post#comments_average_rating' do
        posts = Post.all.eager_group(:approved_comments_count, :comments_average_rating)
        expect(posts[0].approved_comments_count).to eq 1
        expect(posts[0].comments_average_rating).to eq 3
        expect(posts[1].approved_comments_count).to eq 2
        expect(posts[1].comments_average_rating).to eq 4
        expect(posts[2].approved_comments_count).to eq 0
      end

Result:

Failure/Error: expect(posts[2].approved_comments_count).to eq 0

       expected: 0
            got: nil

primary_key mismatch with sum in MySQL

I found a issue with MySQL (or MariaDB in my case) and using sum aggregation.

The Primary keys on the DB query seem to be returned with string primary_keys. Which then does not match the primary_key of the given record for mapping the values.

As the tests only run against sqlite this issue does not seem present. But i thought it might be interesting for someone else.

My change can be seen here: https://github.com/maxigs/eager_group/blob/master/lib/eager_group/preloader.rb#L48
(I just copied the whole class into an initializer in my rails project for now, so i could test it with real database)

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.