Hey I'm just getting back into rails and I've forgotten a lot of the stuff needed to work with some associations.
The Problem:
I have two tables, Customer
with fiels id
and user_id
and User
with field id
. I want to get an array of Users
such that a Customer
's user_id
is the id
of a User
. I know how to do this in SQL but forgot how to do this in Ariel.
Edit
So I think I need to explain a little more. I'm taking over an existing project. There's a one-to-one relation between users and customers however only some users have a related customer entity so calling user.customer
will return nil
most of the time. I need to edit a page that currently lists all users but now I need to list only users that have a customer record.
The relationships have already been created by the previous developer:
class Customer
belongs_to :user, :class_name => "Refinery::User"
end
Refinery::User.class_eval do
has_one :customer
end
and I have the following code in my controller:
def index
# i need to return an array of users, such that the only
# users in this array are the ones with a non-nil customer.
@users = Refinery::User.paginate(:page => params[:page])
end
Any help would be great, thanks!