Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scopes on JsonApi model #526

Open
sqlninja opened this issue May 6, 2019 · 0 comments
Open

Scopes on JsonApi model #526

sqlninja opened this issue May 6, 2019 · 0 comments

Comments

@sqlninja
Copy link

sqlninja commented May 6, 2019

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>>]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant