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

  • Home
  • SEARCH
  • 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 959275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:59:55+00:00 2026-05-16T00:59:55+00:00

I have two classes: class User < ActiveRecord::Base :has_one :foo end class Foo <

  • 0

I have two classes:

class User < ActiveRecord::Base
  :has_one :foo
end

class Foo < ActiveRecord::Base
  :belongs_to :user 
end

The Foo is optional.

I created the following routing:

resources :users do
  resources :foo
end

Which results in the following routes:

GET    /users/:user_id/foo(.:format)              {:controller=>"foos", :action=>"index"}
user_foos POST   /users/:user_id/foo(.:format)              {:controller=>"foos", :action=>"create"}
new_user_foo GET    /users/:user_id/foo/new(.:format)          {:controller=>"foos", :action=>"new"}
GET    /users/:user_id/foo/:id(.:format)          {:controller=>"foos", :action=>"show"}
PUT    /users/:user_id/foo/:id(.:format)          {:controller=>"foos", :action=>"update"}
user_foo DELETE /users/:user_id/foo/:id(.:format)          {:controller=>"foos", :action=>"destroy"}
edit_user_foo GET    /users/:user_id/foo/:id/edit(.:format)     {:controller=>"foos", :action=>"edit"}

Questions:

  1. It seems like the Index and Show actions are redundant. Should one of them be removed? If so, which one?
  2. The :id parameter in the Show action seems unnecessary, as user_id is a foreign key in the foos table and there is only one foo per user. Am I mistaken?
  3. I would like to have a graceful way to route to the New action if there isn’t a foo. One option would be to test @user.foo.nil? in the FooController’s Show or Index action, then redirect to the New action. Is there a better way?

Thanks for your time.

  • 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-05-16T00:59:56+00:00Added an answer on May 16, 2026 at 12:59 am

    If your model has a has_one association, try to set up the route using resource :foo (note the singular method name “resource”, not “resources”). This will set up a singleton resource route (which e.g. has no index action and member actions don’t have an id param since there’s only one member). See also http://apidock.com/rails/ActionController/Resources/resource (2.3 documentation, but applies to 3.0 as well afaik).

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

Sidebar

Ask A Question

Stats

  • Questions 537k
  • Answers 537k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The %g format specifier does its rounding just like %f… May 17, 2026 at 1:46 am
  • Editorial Team
    Editorial Team added an answer You need to escape $ in the regex with a… May 17, 2026 at 1:46 am
  • Editorial Team
    Editorial Team added an answer Have a look at this article: http://www.machackpc.com/featured/flash-silverlight-on-ipadiphone-with-out-installing-any-apps-videos/ Also, perhaps you… May 17, 2026 at 1:46 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have two following classes: public class User { public virtual Guid Id {
I have the following two classes: public class User { public Integer userId; //
Say i have the following two classes: public class User { public int ID
I have two ActiveRecord classes class Product : ActiveRecordBase<Product> { private IList<Usage> _usages =
I have two classes declared as below: class User { public: MyMessageBox dataMsgBox; };
I have two classes Message and User. Message has sender_id and recipient_id both foreign
There are two classes: User and Question A user may have many questions, and
Complete rails newbie trying to get started. I have two classes, Ingredient, and Unit.
In my app I have the classes User, Video, and Vote. Users and Videos
Two classes: <class name=SpreadsheetImportTemplate table=spreadsheetimport_template> <id name=id type=int column=id unsaved-value=0> <generator class=native /> </id>

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.