quick question. 
I'm trying to build a scope without success. I have these models

```
class User < ActiveRecord::Base

 belongs_to :store, required: true
has_many :visits
has_one :company, through: :store

end

class Store < ActiveRecord::Base
  belongs_to :company, required: true
  has_many :users, dependent: :nullify
end

class Company < ActiveRecord::Base
  has_many :stores, dependent: :destroy
  has_many :users, through: :stores
end

class Visit < ActiveRecord::Base
  belongs_to :user
end

```

**edit**

Now I need to draw some chart using Chartkick Gem and the best way is to start from the Visit model. 

I'm using 

```
Visit.group_by_day("started_at", range: 3.month.ago.midnight..Date.today.midnight, format: "%b %-e").order("day asc").count

```

If I need all the visits, but now I need to filter also the visits for company and for store.

I cannot understand how can I get all the visits from users that belong to a specific store or to a specific company. 

The simplest way is:

```
Visit.where("user_id IN (?)", company.users.ids)  

```

It works, but I prefear to write a better scope and the one I write doesn't work.

```
  class Visit < ActiveRecord::Base
  belongs_to :user

  scope :user_by_store, ->(store_id) {
    joins(:user).where('user.store_id' => store_id) 
  }
end

```