Skip to content

Scopes on JsonApi model #526

Open
Open
@sqlninja

Description

@sqlninja

Am I just completely missing something, or do scopes just not work on JsonApi models?

I have the following setup:

class TwitterProfile
  include Her::JsonApi::Model
  use_api TWITTER_API
  collection_path 'profiles'
  type :profiles
  has_many :tweets
  has_many :favorites
  has_many :followers
  has_many :engagement_data, class_name: 'TwitterEngagement'
  ...
end
class TwitterEngagement
  include Her::JsonApi::Model
  use_api TWITTER_API
  type :engagement_data
  belongs_to :twitter_profile

  scope :retweets, -> { where(type: 'retweets') }
  scope :mentions, -> { where(type: 'mentions') }
  scope :favorites, -> { where(type: 'favorites') }
end

***Note, this is not hitting the actual twitter API, but rather my own server where we have some aggregated data, thus the "TwitterEngagement"

When I call
twitter_profile.engagement_data.retweets
I get an error:

NoMethodError: undefined method `retweets' for []:Her::Collection
from /Users/johnhenderson/.rvm/gems/ruby-2.5.0@stacked-sports/gems/her-1.1.0/lib/her/model/associations/association_proxy.rb:11:in `retweets'

But if I pass the scope as an actual predicate:
twitter_profile.engagement_data.where(type: 'retweets')
I get an array as expected:

[#<TwitterEngagement(twitter_engagements/2019-04-21-945481977355833344) date="2019-04-21" profile_id=945481977355833344 count=1 id="2019-04-21-945481977355833344" twitter_profile=#<TwitterProfile(profiles/951230800355254272) name="Justin Houston💰!" screen_name="justnhouston" profile_image="https://pbs.twimg.com/profile_images/10955328872715..." bio="| 4⭐️LB | | FEAR NO MAN | | Instagram @mr.jrich |" id=951230800355254272>>,
 #<TwitterEngagement(twitter_engagements/2019-04-24-1065433729990946818) date="2019-04-24" profile_id=1065433729990946818 count=1 id="2019-04-24-1065433729990946818" twitter_profile=#<TwitterProfile(profiles/951230800355254272) name="Justin Houston💰!" screen_name="justnhouston" profile_image="https://pbs.twimg.com/profile_images/10955328872715..." bio="| 4⭐️LB | | FEAR NO MAN | | Instagram @mr.jrich |" id=951230800355254272>>,
 #<TwitterEngagement(twitter_engagements/2019-04-25-1065433729990946818) date="2019-04-25" profile_id=1065433729990946818 count=1 id="2019-04-25-1065433729990946818" twitter_profile=#<TwitterProfile(profiles/951230800355254272) name="Justin Houston💰!" screen_name="justnhouston" profile_image="https://pbs.twimg.com/profile_images/10955328872715..." bio="| 4⭐️LB | | FEAR NO MAN | | Instagram @mr.jrich |" id=951230800355254272>>,
 #<TwitterEngagement(twitter_engagements/2019-05-01-1718568486) date="2019-05-01" profile_id=1718568486 count=1 id="2019-05-01-1718568486" twitter_profile=#<TwitterProfile(profiles/951230800355254272) name="Justin Houston💰!" screen_name="justnhouston" profile_image="https://pbs.twimg.com/profile_images/10955328872715..." bio="| 4⭐️LB | | FEAR NO MAN | | Instagram @mr.jrich |" id=951230800355254272>>]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions