I have two tables named users and billing. In users I have the usual information, while in billing (contains user_id field for reference) I have things like balances by month for each user. My problem comes from when I want to list the monthly balances. On the monthly balances page I want to display the user(s)’s name, balanace for the month and other misc information, however the billing table only holds the user’s id.
At first i tried
@mondetail = Billings.find_by_date(201204) #April's billing period
@customer = Users.find_by_id(mondetail.user_id)
which works fine for one user but how should I go about this when I want to display the information from both tables with multiple users? Or would it just be best for me to add a user_name field to the billing table?
You don’t need find user’s info in controller. You can get it from
Billingmodel. (Strange that models are plural)It’s will work and without
includesbut you should avoid N + 1 queries problem