{"action":"created","issue":{"url":"https://api.github.com/repos/Eric-Guo/where-or/issues/1","labels_url":"https://api.github.com/repos/Eric-Guo/where-or/issues/1/labels{/name}","comments_url":"https://api.github.com/repos/Eric-Guo/where-or/issues/1/comments","events_url":"https://api.github.com/repos/Eric-Guo/where-or/issues/1/events","html_url":"https://github.com/Eric-Guo/where-or/issues/1","id":107279434,"number":1,"title":"Incompatible structure on seemingly compatible relations","user":{"login":"craysiii","id":1387727,"avatar_url":"https://avatars.githubusercontent.com/u/1387727?v=3","gravatar_id":"","url":"https://api.github.com/users/craysiii","html_url":"https://github.com/craysiii","followers_url":"https://api.github.com/users/craysiii/followers","following_url":"https://api.github.com/users/craysiii/following{/other_user}","gists_url":"https://api.github.com/users/craysiii/gists{/gist_id}","starred_url":"https://api.github.com/users/craysiii/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/craysiii/subscriptions","organizations_url":"https://api.github.com/users/craysiii/orgs","repos_url":"https://api.github.com/users/craysiii/repos","events_url":"https://api.github.com/users/craysiii/events{/privacy}","received_events_url":"https://api.github.com/users/craysiii/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":13,"created_at":"2015-09-18T21:12:43Z","updated_at":"2015-09-21T03:01:36Z","closed_at":null,"body":"I was pretty eager to utilize this gem, but have run into a snag. \r\n\r\n```ruby\r\n# User.rb\r\nclass User < ActiveRecord::Base\r\n  # Include default devise modules. Others available are:\r\n  # :confirmable, :lockable, :timeoutable and :omniauthable\r\n  devise :database_authenticatable, :registerable,\r\n         :recoverable, :rememberable, :trackable, :validatable, :confirmable\r\n\r\n  has_many :wikis\r\n# has_many :wikis, through: :collaborators\r\n  has_many :collaborators\r\n  has_many :collaborated_wikis, through: :collaborators, source: :wiki\r\n\r\n  after_initialize :init\r\n\r\n  def init\r\n    self.role ||= 'standard'\r\n  end\r\n\r\n  def admin?\r\n    role == 'admin'\r\n  end\r\n\r\n  def premium?\r\n    role == 'premium'\r\n  end\r\n\r\n  def standard?\r\n    role == 'standard'\r\n  end\r\n\r\n  def publicize_wikis\r\n    self.wikis.each { |wiki|\r\n      wiki.update_attributes(private: false)\r\n      wiki.collaborators.delete_all\r\n    }\r\n  end\r\n\r\nend\r\n```\r\n\r\n```ruby\r\n# Wiki.rb\r\nclass Wiki < ActiveRecord::Base\r\n  belongs_to :user\r\n\r\n  has_many :collaborators\r\n  has_many :users, through: :collaborators\r\n\r\n  has_paper_trail :only => [:title, :body]\r\n  has_permalink\r\n\r\n  after_initialize :init\r\n\r\n  def init\r\n    self.private = false if self.private.nil?\r\n  end\r\n\r\n  def candidates\r\n    User.where.not(id: self.users).where.not(id: self.user).where.not(role: 'admin')\r\n  end\r\n\r\n  scope :public_wikis, -> { where(private: false) }\r\nend\r\n```\r\n\r\n```ruby\r\n# Collaborator.rb\r\nclass Collaborator < ActiveRecord::Base\r\n  belongs_to :user\r\n  belongs_to :wiki\r\nend\r\n```\r\n\r\n```ruby\r\n# wiki_policy.rb\r\nclass WikiPolicy < ApplicationPolicy\r\n  \r\n  def index?\r\n    true\r\n  end\r\n\r\n  def show?\r\n    super && ( (!record.private) || (record.user == user || user.in?(record.users)) || (user.present? && user.admin?) )\r\n  end\r\n\r\n  def edit?\r\n    show?\r\n  end\r\n\r\n  def destroy?\r\n    user.present? && (record.user == user || user.admin?)\r\n  end\r\n\r\n  def private?\r\n    user.present? && ((user.premium? && (record.user == user || record.new_record?)) || user.admin?)\r\n  end\r\n\r\n  class Scope\r\n    attr_reader :user, :scope\r\n\r\n    def initialize(user, scope)\r\n      @user  = user\r\n      @scope = scope\r\n    end\r\n\r\n    def resolve\r\n      wikis = []\r\n      if user.present? && user.admin?\r\n        wikis = scope.all\r\n      elsif user.present? && user.premium?\r\n        # Here lies the issue - Incompatible relations\r\n        wikis = scope.public_wikis.or(user.wikis).or(user.collaborated_wikis)\r\n      else\r\n        wikis = scope.public_wikis\r\n      end\r\n      wikis\r\n    end\r\n  end\r\n\r\nend\r\n```\r\n\r\nSo User has_many wikis that they own, and has_many collaborated wikis through collaborator. Wiki's has many users through collaborator. When I try:\r\n```\r\nwikis = scope.public_wikis.or(user.wikis).or(user.collaborated_wikis)\r\n```\r\nI get an error from user.collaborated_wikis, which should return the relation of wikis related to this user. user.wikis works the same, by providing the wikis owned by user through collaborators. If I take out the collaborated_wikis call, even though scope.public_wikis and user.wikis are of different classes (ActiveRecord_Relation vs ActiveRecord_Associations_CollectionProxy, respectively) they play well together. If you look at the class of user.collaborated_wikis, it is that of ActiveRecord_Associations_CollectionProxy. Going into the rails console, I can see that the result of user.collaborated_wikis is a list of all the wikis that the user collaborates on, so I know the HMT relation works, I just can't figure out why the gem is saying that the structures are incompatible. Any ideas?"},"comment":{"url":"https://api.github.com/repos/Eric-Guo/where-or/issues/comments/141863825","html_url":"https://github.com/Eric-Guo/where-or/issues/1#issuecomment-141863825","issue_url":"https://api.github.com/repos/Eric-Guo/where-or/issues/1","id":141863825,"user":{"login":"bf4","id":142914,"avatar_url":"https://avatars.githubusercontent.com/u/142914?v=3","gravatar_id":"","url":"https://api.github.com/users/bf4","html_url":"https://github.com/bf4","followers_url":"https://api.github.com/users/bf4/followers","following_url":"https://api.github.com/users/bf4/following{/other_user}","gists_url":"https://api.github.com/users/bf4/gists{/gist_id}","starred_url":"https://api.github.com/users/bf4/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bf4/subscriptions","organizations_url":"https://api.github.com/users/bf4/orgs","repos_url":"https://api.github.com/users/bf4/repos","events_url":"https://api.github.com/users/bf4/events{/privacy}","received_events_url":"https://api.github.com/users/bf4/received_events","type":"User","site_admin":false},"created_at":"2015-09-21T03:01:36Z","updated_at":"2015-09-21T03:01:36Z","body":"So, what I put in the gist is what we're using in production, fwiw"}}