Coder Social home page Coder Social logo

Comments (7)

bullfight avatar bullfight commented on May 29, 2024

I took some time to inspect the code for #lib/grape/entity

I see that represent expects an array of objects or a single object

    # This convenience method allows you to instantiate one or more entities by
    # passing either a singular or collection of objects. Each object will be
    # initialized with the same options. If an array of objects is passed in,
    # an array of entities will be returned. If a single object is passed in,
    # a single entity will be returned.
    #
    # @param objects [Object or Array] One or more objects to be represented.
    # @param options [Hash] Options that will be passed through to each entity
    #   representation.
    def self.represent(objects, options = {})
      if objects.is_a?(Array)
        objects.map{|o| self.new(o, {:collection => true}.merge(options))}
      else
        self.new(objects, options)
      end
    end

Would it be reasonable to add a condition to map an ActiveRecord::Relation?

from grape.

yesezra avatar yesezra commented on May 29, 2024

I'd caution against checking whether the object is an ActiveRecord::Relation – why not do the check for Enumerable? That would, hopefully, allow compatibility with other ORMs.

from grape.

amrnt avatar amrnt commented on May 29, 2024

same w/ mongoid #76

from grape.

mbleigh avatar mbleigh commented on May 29, 2024

The problem with checking for Enumerable is that a Hash is also an Enumerable and a hash should not have the same behavior mapped to it. I'm open to suggestions but not entirely sure what to do to solve the issue reliably for all cases.

from grape.

mbleigh avatar mbleigh commented on May 29, 2024

OK, I think I know what needs to be done. This will solve the problem in ActiveRecord and should solve the problem in any well-implemented ORM. I'm documenting this here because I don't have time to make the change and test it right now:

Grape should do a respond_to?(:to_ary) on the incoming object and call it if it exists. #to_ary should only exist on objects that can be considered an array, not objects that can be converted to an array (so Hashes, which do respond to #to_a, do not respond to #to_ary).

objects = objects.to_ary if objects.respond_to?(:to_ary)

If someone sends a pull with tests before I get to it, I will accept. This should fix #76 as well.

from grape.

simb avatar simb commented on May 29, 2024

I have confirmed that changing that to check for Enumerable solves the error I am having.

from grape.

mbleigh avatar mbleigh commented on May 29, 2024

This should be fixed now.

from grape.

Related Issues (20)

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.