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 8789161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:19:29+00:00 2026-06-13T22:19:29+00:00

Projects have users. Users have expenses. I have a screen for the currently signed

  • 0

Projects have users.
Users have expenses.

I have a screen for the currently signed in user to add new expenses, my controller forces the user key based on current_user.

Now I am needing to allow users to work within a particular project, and enter their expenses for that project all at once, basically “enter” a project-expense screen, to limit the scope of the expenses shown in that screen by the project they selected.

(Similar to the way basecamp makes you pick a project you work on)

I do want to use this view for other things, and would like to make it as DRY as possible.

Was just wondering, what is the proper way to have a user select a project, and then persist that choice through their use of my existing expenses screen.

EDIT – I would like to be able to pick a project, and then have all the functionality on the expenses page pass along that project through to the controller methods.

So I could end up with /project/45/expenses …. etc

Thanks!

class Project < ActiveRecord::Base
  attr_accessible :name, :active

    has_many :expenses, foreign_key: :project_id

end

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

  belongs_to :project
    belongs_to :user
    belongs_to :expense_type
    has_one :expense_approval

end

class User < ActiveRecord::Base
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :name, :email, :password, :password_confirmation, :remember_me

  has_many :expenses, foreign_key: :user_id
end
  • 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-13T22:19:31+00:00Added an answer on June 13, 2026 at 10:19 pm

    You have a mildly complex set of relationships. Now you have to think of the query semantics you’ll require in your app.

    Remembering your original post subject ‘Nesting… needed?’

    That depends on the various query semantics you’d like in your app.

    The most obvious ‘nested’ relationship is:

    class Project < ActiveRecord::Base
      attr_accessible :name, :active
      has_many :expenses, foreign_key: :project_id
    end
    

    So if in your app, it only ever makes sense to query expenses that belong to a project, you might consider creating nested routes i.e.:

    config/routes.rb:

    resources :project do
      resources :expense
    end
    

    Assume that is the ONLY route in your app. The only way to reference any expense is to do it through the project it belongs to. So this query would be available:

    GET /project/101/expenses # index all expenses for project with id 101
    

    BUT, look at the NUMEROUS routes generating with the above entry in routes.rb. To see, run ‘rake routes’ with the above nested routes included in ‘routes.rb’ , then remove the above entry from ‘routes.rb’ and run ‘rake routes’ again.

    The nested routes adds ALOT of routes. It may add MANY routes you don’t need. If that is the case, you may want to take a different approach. DON’T use the nested routes, but instead HAND CRAFT only the routes you really need.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When I have NSString with /Users/user/Projects/thefile.ext I want to extract thefile with Objective-C methods.
I have a database where the users have different projects. A user can have
I have users and projects. User can have many projects and project can have
I have an application which has a Projects table and a Users table. In
I have saved an image path in database like so: C:\Users\3embed\Documents\Visual Studio 2010\Projects\HeritageWeb\HeritageWeb\Images\startbutton.png I
Is there possible, to have some projects under a certain user.name and user.email and
I have the following model setup - a User is interested in projects in
I have apps Users and Projects and would like to define another app called
I have tables: users {id, name} projects {id, name} roles {id, name} projects_users {id,
I have an app that has Basecamp-style subdomains, that is, I have Projects, Users,

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.