Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
    Keith Gould
    @keithmgould
    Hey @scottgonzalez I meant to mention this off the bat but the first thing I tried was overriding the id method in the resource, but that caused a spooky silent error where the data object came back empty. Have you ever tried? I'm guessing its easy to reproduce.
    I'll be caring both about finding via UUID, and also sending down those UUIDs, so I'll def look into the finder stuff you sent, but I'm still at a loss as to how to send down UUIDs (as the ID), since I can't override the id method (as mentioned).
    Keith Gould
    @keithmgould
    Thank you for the help, btw, both yesterday and today. Are you a core dev of JR?
    Scott González
    @scottgonzalez
    @keithmgould Well, not quite. I’m a committer, but very shortly after getting commit access, there were big changes at work and I haven’t had much time to work on it.
    But I know a decent amount about how it works, so I try to at least help out here when I can since it’s a much smaller commitment.
    I haven’t tried overriding the id method before. I’d need to do quite a big of digging to see everything it would affect.
    Out of curiosity, why don’t you just change that model to use the UUID as the actual primary key?
    Another approach that might work out pretty well would be to create another model that just wraps the original model, but uses the UUID as the primary key.
    You’d probably only need to override a few methods, like self.find, id, id=, etc.
    Keith Gould
    @keithmgould
    Yeah, I could do either of those. To answer your question though, I don't like making the UUID a primary key because it does not feel beneficial outside the world of the API. Internally (inside the app), integers are just fine, and fast, and small, and easy for a human dev to see and type when debugging. These are a lot of perks to an int PK. And again no benefits inside the app for UUIDs. So where I've landed architecturally over the years is to have internal ints, and an external UUID, because to surface an integer PK to the world is not good for a few reasons.
    I'd rather not write a wrapper, it feels like thats a lot make JR happy. But I'll go with that for now unless you find a way to make it work with overriding id, which I'd prefer.
    Keith Gould
    @keithmgould
    This message was deleted
    Keith Gould
    @keithmgould
    Also, are there examples I can find where JR sits in front of a service layer? Like lets say I have a service in the app that accepts user info from a client, does a few things, and returns something to the client. I don't want this behavior to be defined in a single model, since the behavior spans multiple models. So traditionally I create a service in the app that handles this. Can JR reference this service? What methods would need to be defined in the service? Are there examples?
    Keith Gould
    @keithmgould
    (Also the Serialize portion of Documentation is out of date, I think. there is no longer a serialize_to_hash method.
    Keith Gould
    @keithmgould
    I imagine its something like this now but what I have is not working:
        survey = Survey.first
        resource = SurveyResource.new(survey, nil)
        serializer = JSONAPI::ResourceSerializer.new(SurveyResource)
        serializer_set = JSONAPI::ResourceSet.new
        serializer_set.populate!(serializer, nil, {})
        foo = serializer.serialize_resource_set_to_hash_single(serializer_set)
        render json: foo
    The above is what I'm thinking about doing to address the service layer question...
    Lele Forzani
    @wmlele
    Using 0.10.x. I have a resource with several filters that may be applied to the collection, that return a subset of the entire collection, which is also paginated. Is there a way to add metadata that are calculated on the full set matching the filters?
    Paul Vilchez
    @pvilchez
    @keithmgould Have you had any luck with the Serializer? I am just running across the same issue.
    Iuval
    @iuval

    Hello, I'm looking into scoping associated resources and can't make it work.
    I'm trying records_for it's not being called.

    class OrganizationResource < JSONAPI::Resource
      has_many :accounts
    
      # I've tried this
      def records_for_accounts(relation_name, options = {})
        byebug
        # Not being called
        Pundit.policy_scope!(options[:context][:current_user], _model.public_send(relation_name))
      end
    
      # And this
      def records_for(relation_name, options = {})
        byebug
        # Not being called
        Pundit.policy_scope!(options[:context][:current_user], _model.public_send(relation_name))
      end
    
      # This is working but it returns empty hashes on the included list 
      # (one hash per resource that was not allowed to be shown)
      def records_for_populate(options = {})
        Pundit.policy_scope!(options[:context][:current_user], _model_class.all)
      end
    end

    Any idea what's going on?
    Is this deprecated or something?

    The endpoint is /organizations?include=accounts

    Adam Robertson
    @arcreative
    Is there no after_update callback for processors?
    at least on 0.9.x?
    it makes more sense that there isn't one for after_destroy since it returns :no_content, but I find myself in a position to add some meta to a response after an update
    Keith Gould
    @keithmgould
    @pvilchez hi. Not really, and I decided that while this gem is great for a restricted use-case, I found myself fighting against it a bit more than was comfortable. So I switched to the jsonapi-rails gem, which I am liking a lot. http://jsonapi-rb.org/. They also have a gitter: https://gitter.im/jsonapi-rb/Lobby. Again I think jsonapi-resources is very cool if your use-case falls into its parameters.
    1 reply
    Matic Jurglič
    @matixmatix

    Hi! I'm looking for some help. Is it possible to add a has_one relationship to a resource but implement it with a custom query?

    My use case is the following: I have a UserResource and would like to include one Note - the most recent one. Note is a table with foreign keys user_id, coach_id. So I would like the has_one :note to go to the notes table and find the most recent one and attach it to the response. Is this possible with jsonapi-resources?

    Matic Jurglič
    @matixmatix
    I tried this, but I get an exception: Internal Server Error: undefined method 'note_id'
    has_one :note, eager_load_on_include: false
      def note
        note = @model.notes.first
        Api::V1::NoteResource.new(note, context)
      end
    Scott González
    @scottgonzalez
    @matixmatix The gem cares about the IDs, so that’s the method you have to implement.
    Matic Jurglič
    @matixmatix
    @scottgonzalez thanks for your reply. Could you provide more info, please? What kind of method and where?
    Scott González
    @scottgonzalez
    Just add note_id to the resource.
    23 replies
    You’ll also need to handle the setter if the relationship is writable in your API.
    Chris Trinh
    @chtrinh
    Hi, how do I add a scope to a relationship that I'm including, e.g. api/item/16?include=asset? Currently my workout around is to use a default scope in Asset model. But I'm trying to avoid this.
    Chris Trinh
    @chtrinh
    my first thought was to subclass the ApplicationRecord and apply the default_scope only there to keep it isolated, but that seems iffy. thoughts/advice?
    Chris
    @chrisosborn
    Hello; does anyone know if there is a way to have configuration values apply to some resources/endpoints but not to others? (In particular, we are interested in maximum_page_size.) Thanks in advance for any help!
    Scott González
    @scottgonzalez
    @chrisosborn I believe you can create change the paginator using ResourceClass.paginator(paginator)
    You’d want to inherit from OffsetPaginator and override verify_pagination_params (https://github.com/cerebris/jsonapi-resources/blob/d185ebb76cd20f17d82e05e3298ee291f06aead1/lib/jsonapi/paginator.rb#L108-L119)
    Chris
    @chrisosborn
    @scottgonzalez Thank you! I will check that out!
    Matic Jurglič
    @matixmatix
    is it possible to convince JR to set an instance variable for the resource? for example, if I have a NotesController, I would like to have the @note instance variable set (for show, edit, destroy actions). (I'd like this because I have CanCanCan for resource authorization and want to avoid double loading). Any tips? 🙏
    Scott González
    @scottgonzalez
    @matixmatix Do you mean can the controller set an instance variable on the resource?
    If so, you can use context. It’s shared between the controller and resource.
    Alex Oliveira
    @kurko
    I'm looking for a way of adding to meta, {"pageCount"=>2, "totalCount"=>4}. Any tips. I'm reading the source code and trying to figure out where that is, but can't so far.
    Alex Oliveira
    @kurko
    Ok, I found https://gitter.im/cerebris/jsonapi-resources?at=5cdc5057b149ca509898a326 from may/2019, v0.9. Not sure it'll work on v0.10. v0.10 dropped the Processors doc page so I don't know what a processor is (IMHO everything everywhere is processed by something, so that name is confusing). Going to try that. Thx.
    Julius-Rapp
    @Julius-Rapp
    @kurko: Are you aware of the config options? https://jsonapi-resources.com/v0.10/guide/configuration.html#Defaults
      # Metadata
      # Output record count in top level meta for find operation
      config.top_level_meta_include_record_count = false
      config.top_level_meta_record_count_key = :record_count
    
      config.top_level_meta_include_page_count = false
      config.top_level_meta_page_count_key = :page_count
    Adam Robertson
    @arcreative
    i know this has probably been covered before, but is there any way to make a custom model method behave like a has_many?
    i have a method on my model that returns a relation, but can't be included with JR because it's not a real association
    Alex Oliveira
    @kurko
    Hi everyone. I'm trying to debug a weird issue. I have a relationship, and although I'm using always_include_linkage_data: true, the relationship's data is not present in the relationships key of the parent resource. I'm running byebug throughout the gem's code, and it looks like relationship_data is always empty in https://github.com/cerebris/jsonapi-resources/blob/master/lib/jsonapi/resource_serializer.rb#L264. I'm not sure I'm missing something. My resources are pretty standard. Does anyone have any debugging tips?
    Alex Oliveira
    @kurko
    Can't make resource linkage work ;(
    Scott González
    @scottgonzalez
    @kurko Do you have the relationship defined in the resource?
    Alex Oliveira
    @kurko
    Yes. Simplest relationship you can imagine. Tried all sorts of things, joining the relationship in records_for_populate (e.g includes). Had byebug spread across the gem, relationships related variables/state are always empty. I decided to punt on this and just let it return links. I'll get back to it when I'm about to deliver the API as to not get blocked.
    Alex Oliveira
    @kurko
    @Julius-Rapp apologies I didn't respond. I didn't see your message.
    Benjamin Fleischer
    @bf4
    @kurko we're still on 0.9 but I'd be surprised if the vanilla case didn't work. (when we tried to upgrade to 0.10 soon after stable came out our blocker was that we treat 'calculated' resources as posts/create and the way that was being handled in JR had changed in a way I was unable to resolve at the time. ) do you have a gist or something to share? routes set up with the jsonapi_resources macro, controllers exists, resources exist? it's possible the docs are wrong (seemed at the time at the 0.10 docs were just copied from 0.9 even when wrong)