Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
    Joe Gaudet
    @joegaudet
    We need that records call to somehow load the related records instead of using the join.
    yasser007-munic
    @yasser007-munic
    I'm having some difficulties using json-api version 0.10.4. in the old versions i used to use resource_for_xxxxx to fetch the related resource, make some modifications then return the result as a response to some requests. But now in this version this function record_for_xxxx is no longer used, and looking to your documentation i found out that i can use apply_join to change how the resources are joined, but this won't help me, i need to make changes after the join. does any one have an idea to how to do it ?
    Larry Gebhardt
    @lgebhardt
    @yasser007-munic What type of changes do you need to make? The changes in 0.10.x split the fetching of resources into two phases, the first filters and paginates the records to get the ids and cached keys needed for the results. The second phase gets the model instances to fill in the models that were not found in the cache. If you only need to make joins for bringing in other models for serializing the resource you can override the records_for_populate method. When this is called you can assume the filtering/permissions checks took place in the first phase so you should not need to include those.
    yasser007-munic
    @yasser007-munic
    @lgebhardt it's a little bit complicated, let's say i have a modelmodel_x this model has an attribute name, and another model model_y the second model has_many of the first model (there is an association table between them). so whenever i want to fetch all model_ys of a model_x(/model_xs/id/model_ys), i need to change the name of every model_x by adding a prefix to the attribute name, this prefix is stored in the association table between model_y and model_x
    so in the last version. i was able to do it using records_for_model_x in the model_y_resources
    Louis
    @louiscode:matrix.org
    [m]

    hi guys.
    Could anyone find out the way to alternative the way detect id for JSONAPI::Resource

    I have model but id was nil value. i want to use object_id to detect instead. Do we have anyway to do it

    1 reply
    Anton
    @Ivanov-Anton

    Hi everyone I, my name Is Anton Ivanov and I have suggestion regardlin relationships.

    if a relationship is provided in the data member of a PATCH request and relationship value is empty array then back-end should render bad_request response today our gem render 500 Internal Server Error

    undefined method `keys' for []:Array lib/jsonapi/request.rb:607 block in verify_permitted_params

    If no one is against this behavior, then I can try to implement it in a new pull request.

    Artur
    @artur79
    Hi, did anyone experienced that error after updating from 0.10.4 to 0.10.5 ? I'v got in in two projects so it's rather gem related.
    2 replies
    Rswag::Specs::UnexpectedResponse: Expected response code '500' to match '200' Response body: {"errors":[{"title":"Internal Server Error","detail":"Internal Server Error","code":"500","status":"500", "meta":{"exception":"Mysql2::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.\"id\" AS \"users_id\", `users`.`id` FROM `users` WHERE `users`.`id` = 313 ORDER BY' at line 1", "backtrace":["/home/circleci/project/vendor/bundle/ruby/3.0.0/gems/mysql2-0.5.3/lib/mysql2/client.rb:131:in `_query'", "/home/circleci/project/vendor/bundle/ruby/3.0.0/gems/mysql2-0.5.3/lib/mysql2/client.rb:131:in `block in query'", "/home/circleci/project/vendor/bundle/ruby/3.0.0/gems/mysql2-0.5.3/lib/mysql2/client.rb:130:in `handle_interrupt'", "/home/circleci/project/vendor/bundle/ruby/3.0.0/gems/mysql2-0.5.3/lib/mysql2/client.rb:130:in `query'", "/home/circleci/project/vendor/bundle/ruby/3.0.0/gems/activerecord-6.1.3.2/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:206:in `block (2 levels) in execute'","/home/circleci/project/vendor/bundle/ruby/3.0.0/gems/activesupport-6.1.3.2/lib/active_support/dependencies/interlock.rb:48:in `block in permit_concurrent_loads'","/home/circleci/project/vendor/bundle/ruby/3.0.0/gems/activesupport-6.1.3.2/lib/active_support/concurrency/share_lock.rb:187:in `yield_shares'","/home/circleci/project/vendor/bundle/ruby/3.0.0/gems/activesupport-6.1.3.2/lib/active_support/dependencies/interlock.rb:47:in
    Gabriel Fortuna
    @gee-forr
    hey all (and @lgebhardt in particular :) ) I'm very happy that I've started a new project, and get to work with JR again... I've got a small problem though. We've moved away from exposing database primary keys as the external facing id for our records, and have started using HashIDs.
    13 replies
    Gabriel Fortuna
    @gee-forr
    rats... i've gone through the code, and it looks like there's a big implicit reliance on a hardcoded .id field pretty much everywhere. I don't think what I'm asking for above is possible without a major refactor to JR. Is my assessment correct, @lgebhardt ?
    Joe Gaudet
    @joegaudet
    @lgebhardt ever thought of using something like this to optimize JR. https://blog.crunchydata.com/blog/generating-json-directly-from-postgres
    You’d probably need to mark a resource as comming directly from the DB.
    RobL
    @braindeaf
    Hey, couple of questions if I may. Is ActiveRecord going to be required from now on. We have two apps that don't use it and therefore this is going to be something we'll need to hack
        def process_operations(transactional)
          if transactional
            run_callbacks :transaction do
              ActiveRecord::Base.transaction do
                yield
              end
            end
          else
            begin
              yield
            rescue ActiveRecord::Rollback
              # Can't rollback without transaction, so just ignore it
            end
          end
        end
    RobL
    @braindeaf

    Secondly, I'm trying to set up a singleton resource

    class Api::V1::MeResource < JSONAPI::Resource
      model_name 'User'
    
      singleton singleton_key: (lambda do |context|
        key = context[:current_user].try(:id)
        raise(JSONAPI::Exceptions::RecordNotFound, nil) if key.nil?
    
        key
      end)
    
      attributes :uuid, :full_name, :email
    
      relationship :tenant, to: :one
    end

    by default the resource is assumed to be a 'me', I want it to be 'users'. But maintain /api/v1/me

    {
      "data": {
        "id": "000001",
        "type": "mes",
        "links": {
          "self": "https://demo.test/api/v1/mes"
        },
        "attributes": {
          "uuid": "ef242d051684",
          "full_name": "Mr Rob Lacey",
          "email": "rob.lacey@fluff.co.uk"
        },
        "relationships": {
          "tenant": {
            "links": {
              "self": "https://demo.test/api/v1/mes/relationships/tenant",
              "related": "https://demo.test/api/v1/mes/tenant"
            }
          }
        }
      }
    }

    This fixes the type, but in order to support /me/relationships/tenant the urls can't be using /users, that's the job of UserResource, etc

    
      def self._type
        :users
      end

    So this is weird now. I am not even user the links should point at a nested relationship if tenant is also a first class citizen at /api/v1/tenants/1

    {
      "data": {
        "id": "000001",
        "type": "users",
        "links": {
          "self": "https://demo.test/api/v1/users"
        },
        "attributes": {
          "uuid": "ef242d051684",
          "full_name": "Mr Rob Lacey",
          "email": "rob.lacey@fluff.co.uk"
        },
        "relationships": {
          "tenant": {
            "links": {
              "self": "https://demo.test/api/v1/users/relationships/tenant",
              "related": "https://demo.test/api/v1/users/tenant"
            }
          }
        }
      }
    }
    Antonis Tzorvas
    @atzorvas
    where would you test your immutable resources? (the fact that’s immutable)
    in a request or resource spec?
    Ekkehard Ehrenstein
    @Ekkstein
    Hello Everybody. Im new and unexperienced, guidance and advice is welcome. To my question. I have added the gem to my repo and am now receiving segmentation faults. First on ruby 2.6.5p114 when using a resource generator. Then I upgraded to ruby 2.7.0 and this segmentation fault disappeared. Instead I am now getting them when I shut down the console or the dev server. This gem would be a huge help in my development timeline. Can anybody please help me with this?
    Antonis Tzorvas
    @atzorvas
    how can I disable index action? :eyes:
    My understanding is that I’d have to override the method in ResourceController and raise from there only: [:show]
    john
    @john:fireballxl20.com
    [m]
    Hello, is there a reason that belongs_to is deprecated in favor of has_one? Why not match the ActiveRecord api?
    Artur
    @artur79
    hi, what approach do you use to set in resource class attributes dynamically ? I'm trying attribute -> { ldap_auth_bind_key_field }, if: :uses_ldap? but got error undefined method `to_sym' for #<Proc:0x00007f7f3ca773b8. Second part after if: works
    Artur
    @artur79
    I'v got of course defined, self.ldap_auth_bind_key_field class method
    Artur
    @artur79
    I'd like to prevent error 500 and use own one in case when one of the tags does not exist, is there any way to prevent it, at the moment I get
    [www] [3cd0412f-be18-4dd7-b971-15a2fb60e52e] Internal Server Error: Couldn't find all Tags with 'id': (2, 4818192505) (found 1 results, but was looking for 2). Couldn't find Tag with id 4818192505. /bundle/vendor/ruby/3.0.0/gems/activerecord-6.1.4/lib/active_record/relation/finder_methods.rb:362:in `raise_record_not_found_exception!' /bundle/vendor/ruby/3.0.0/gems/activerecord-6.1.4/lib/active_record/associations/collection_association.rb:69:in `ids_writer' /bundle/vendor/ruby/3.0.0/gems/activerecord-6.1.4/lib/active_record/associations/builder/collection_association.rb:66:in `tag_ids=' /bundle/vendor/ruby/3.0.0/gems/jsonapi-resources-0.10.4/lib/jsonapi/basic_resource.rb:1104:in `call' /bundle/vendor/ruby/3.0.0/gems/jsonapi-resources-0.10.4/lib/jsonapi/basic_resource.rb:1104:in `block in define_foreign_key_setter' /bundle/vendor/ruby/3.0.0/gems/jsonapi-resources-0.10.4/lib/jsonapi/basic_resource.rb:322:in `_replace_to_many_links' /bundle/vendor/ruby/3.0.0/gems/jsonapi-resources-0.10.4/lib/jsonapi/basic_resource.rb:90:in `block in replace_to_many_links' /bundle/vendor/ruby/3.0.0/gems/jsonapi-resources-0.10.4/lib/jsonapi/basic_resource.rb:60:in `block in change' /bundle/vendor/ruby/3.0.0/gems/activesupport-6.1.4/lib/active_support/callbacks.rb:98:in `run_callbacks'
    not sure actually is it triggered by rails side or jsonapi resources one
    Ruben Santos
    @soltex1
    anyone knows how to fix this issue: undefined method 'serialize_to_hash' for #<JSONAPI::ResourceSerializer:0x00007f4cfc8ba240>
    here's what I am doing: JSONAPI::ResourceSerializer.new(ProjectTypeResource).serialize_to_hash(ProjectTypeResource.new(ProjectType.find(1), nil))
    Ruben Santos
    @soltex1
    it looks like adding gem 'jsonapi-resources' to the Gemfile will install the version 0.10.5, that one doesn't contain the method serialize_to_hash, so not sure why the documentation are using a method that is not present in the latest version
    Joe Gaudet
    @joegaudet
    I think it was added back in, might not be on release yet.
    @soltex1 ^^^
    Ruben Santos
    @soltex1
    @joegaudet thank you for letting me know! I ended up using an older release
    Joe Gaudet
    @joegaudet
    @soltex1 should give edged a go, the caching and lookup is quite a bit feaster.
    Joe Gaudet
    @joegaudet
    @lgebhardt observing something weird, seems read_multi on the cache adapter is trimming my dates that have been formatted to include higher precision.
    Screen Shot 2021-08-24 at 12.04.50 PM.png
    Screen Shot 2021-08-24 at 12.05.13 PM.png
    Not really sure how it would do that.
    Joe Gaudet
    @joegaudet
    Nevermind, I figured it out.
    Ignore me.
    Anuj Patel
    @patelanuj28
    Anyone knows the good library to parse JSON API response via ruby gem?
    There is one in javascript: https://github.com/beauby/jsonapi-datastore however I am looking for similar for ruby
    Appreciate for recommendation
    Joe Gaudet
    @joegaudet
    @lgebhardt since upgrading to rails 6, while still on .9 branch we observe some very strange caching behaviours, are you aware of anything that could be causing that?
    Were there changes to the caching mechanism we aren’t taking into account?
    Vinícius
    @vinidotdev

    Hi, everyone. Could you help me? I have the following context

    class Country < ApplicationRecord; end
    
    class Brazil < Country; end # and many others
    class Api::V4::CountryResource < JSONAPI::Resource
      immutable
      model_name 'Country'
      attributes :name
    end

    When I hit /api/v4/countries?page%5Bnumber%5D=1&page%5Bsize%5D=100 it returns

    "exception": "JSONAPI: Could not find resource 'brazil'. (Class Api::V4::BrazilResource not found)",
    Vinícius
    @vinidotdev
    So, I created all countries resources and it works
    Vinícius
    @vinidotdev
    "data": [
            {
                "id": "1",
                "type": "brazils",
                "attributes": {
                    "name": "Brasil"
                }
            },
            {
                "id": "2",
                "type": "mexicos",
                "attributes": {
                    "name": "México"
                }
            }
    }...
    But it is not the best solution
    Avery Bloom
    @averydev
    Hi there! I was looking for a way to do a filter but only return a single record rather than a set of records but couldn't sort out a syntax that did what i was expecting. I'd appreciate suggestions!
    Returning a single record from a filter throws an error looking for the .order method
    David Valaz
    @davidvalaz

    Hey All! Happy evening from over here in London.

    Seem to be getting a weird error when I try to version my API. Running rails server throws up this error:

    /Users/David/.asdf/installs/ruby/3.0.0/lib/ruby/gems/3.0.0/gems/zeitwerk-2.4.2/lib/zeitwerk/loader/callbacks.rb:18:in `on_file_autoloaded': expected file /Users/David/Code/Ruby/my_project/app/controllers/connector_controller.rb to define constant ConnectorController, but didn't (Zeitwerk::NameError)

    Everything works just find when I don't start adding module Api and module V1 to my classes

    David Valaz
    @davidvalaz
    Oh and I'm using v10 btw