views:

197

answers:

2

I have 2 tables I need to join but the user_id column value is not the same in both tables. So I want to do something like this:

In my controller 4 will be substituted with current_user.id

select * from sites join pickups on sites.id = pickups.site_id where sites.user_id = '4'

But using an ActiveRecord Find.

Here are my associations:

class Site < ActiveRecord::Base
  belongs_to :user
  has_many :pickups

class Pickup < ActiveRecord::Base  
 belongs_to :site   
 belongs_to :user

class User < ActiveRecord::Base
    has_one  :profile
    has_many :pickups
    has_many :sites

Thanks in advance!

+1  A: 

try this:

sites = current_user.sites.find(:all, :include => [:pickup])
Tam
+2  A: 

If you add this to your user model:

has_many :site_pickups, :through => :sites, :source => :pickups

You can do

current_user.site_pickups
jonnii
In my model I had to do :`has_many :site_pickups, :through => :sites, :source => :pickups`
JetShred