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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:29:28+00:00 2026-05-28T00:29:28+00:00

If I have a nested resource like so: resources :users resources :posts end and

  • 0

If I have a nested resource like so:

resources :users
  resources :posts
end

and a user has_many posts, it is possible to have Rails start numbering based on the parent association in the URL? For example, currently, nesting resources just grabs the ID:

@user.posts.find(params[:id])

This correctly namespaces the posts, only allowing posts from @user… however, is there a way such that the post_id is independent? I.E. I want each user’s posts to start at 1, where:

/users/1/posts/1
/users/2/posts/1

Actually refer to two different posts?

  • 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-28T00:29:29+00:00Added an answer on May 28, 2026 at 12:29 am

    It can be quite a bit of work, but basically you can do it with these steps:

    1. Create a migration to add a new attribute to store the specific user-post count. (I used user_post_id)
    2. Override Post‘s to_param method to use the new value you just created. (It has to be a string.)
      • to_param is the method that the url and path helpers use.
    3. Create a before_save filter that will actually increment the user_post_id value for each new post.
    4. Change all your controller methods to find on user_post_id

      @user = User.find(params[:user_id])
      @post = @user.posts.where(:user_post_id => (params[:id])).first
      
    5. Change all your Views that might not work now

    You can see the source here: Custom Nested Resource URL example

    Code

    migration:

    class AddUserPostIdToPosts < ActiveRecord::Migration
      def change
        add_column :posts, :user_post_id, :integer
      end
    end
    

    post.rb:

    class Post < ActiveRecord::Base
      before_save :set_next_user_post_id
      belongs_to :user
    
      validates :user_post_id, :uniqueness => {:scope => :user_id}
    
      def to_param
        self.user_post_id.to_s
      end
    
    private
      def set_next_user_post_id
        self.user_post_id ||= get_new_user_post_id
      end
    
      def get_new_user_post_id
        user = self.user
        max = user.posts.maximum('user_post_id') || 0
        max + 1
      end
    end
    

    A couple controller methods
    posts_controller.rb:

    class PostsController < ApplicationController
      respond_to :html, :xml
    
      before_filter :find_user
    
      def index
        @posts = @user.posts.all
        respond_with @posts
      end
    
      def show
        @post = @user.posts.where(:user_post_id => (params[:id])).first
        respond_with [@user, @post]
      end
      ...
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a nested resource in my routes.rb like this: map.resources :users, :only =>
I have a nested resource, setup like this: resources :chickens do resources :eggs end
I have the following rails 3 nested resource: resources :books do resources :authors end
In Rails 3 edge, I have set up two nested resources like this: config/routes.rb
I'm using Rails 2. I have resources nested like this: - university_categories - universities
I have a nested route like so: resources :apps do resources :issues end the
I have queried on user posts (nested resources, obviously) and have a long list
I have a nested model structure that looks like this: resources :users, :path =>
Let's say I have a nested resource like in the Rails guide example: http://guides.rubyonrails.org/routing.html#nested-resources
I have some nested models that look something like: class Company has_many :managers end

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.