Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8798599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:07:56+00:00 2026-06-14T00:07:56+00:00

Thanks to some help in another question, I now have a nested resource setup,

  • 0

Thanks to some help in another question, I now have a nested resource setup, and it is nearly working the way I need it to. This is a follow-up question about the controller.

Expenses have both a user, and a project that they belong to.

I would like to visit /projects/5/expenses, and see a list of all the expenses for that project, (which IS working), but also have it sensitive to the user that is currently signed in, so they only see their own expenses.

first the models:

class Expense < ActiveRecord::Base
  attr_accessible :amount, :project_id, :user_id

  belongs_to :project
  belongs_to :user

end

each of the other models has “has_many :expenses”, to complete the relationship.

so my route looks like:

  resources :projects do
    resources :expenses
  end

And

class ExpensesController < ApplicationController

    def index
      @user = current_user  
      @project = Project.find(params[:project_id])
      @expense_list = @project.expenses.all  
end

How can I filter my @expense_list further by only showing the current_user’s expenses?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T00:07:57+00:00Added an answer on June 14, 2026 at 12:07 am

    You need a an additional condition to query the expenses based on the user that they belong to.
    I would suggest that you create a scope in your Expense model

    scope :for_user, lambda{ |user| 
          where( :user_id => user.id )
       }
    

    And you can do this in the controller:

    @expense_list = @project.expenses.for_user(current_user).all  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question is related to another question of mine. Thanks to some help I
Thanks to some help I received yesterday I've got some dynamic summing working on
Hi and thanks for your attention. First some background on the question: I have
I'm going to give this another try because my last question might have been
Could do with some help with this question.I am using the system.io, and I
This question is with reference to my another question Auto complete not working .
I have found lot's of variations for this question but just wanted some clarity
I didn't mean it to rhyme! But I need help. I have done some
I am working on an android project right now and have a question about
This may be too ambiguous of a question, but I'm looking for some help

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.