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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:20:16+00:00 2026-05-31T16:20:16+00:00

This is probably a really simple question but one I’ve never quite worked out

  • 0

This is probably a really simple question but one I’ve never quite worked out myself being still fairly new to rails.

I’ve setup devise on one project locally that works very well, however at the time I couldn’t think of a way to set authentication up for my models properly, I didn’t want the user to have to edit the user details to edit their respective model details at the same time so I put a hidden form in the models form containing the value for the ‘current_user.id’ to ensure it always saved to the logged in user – however obviously I’ve realised that the value of this form could be changed to anything via the source and any data re-assigned to any user.

What is the proper way to easily setup models that belong to users, which can be created/edited/deleted etc. against that user independently without having to save user details alongside in the ‘accepts_nested_attributes_for’ way I’ve seen before with things like this?

I guess I’m just looking to dig a bit deeper and understand how to relate these models to a user, but the models work completely independently off themselves and don’t require user data from the Users model to be saved.

An example data structure is:

User -> Posts and Posts -> Comments where posts/comments can be added/edited deleted without having to change the original user data, an email/password is just used for authentication purposes.

I appreciate the object structure could quite easily be:

User:[{
    Posts:[{
        "name":"test",
        "description":"test"
        {
        "name":"test2",
        "description":"test2"
        }]
    }]
}]

But in this specific example I would want Posts to be a separate model with their own comments on each and the only relationship to be that the post in question was created by “Joe Bloggs” or UserID 4.

Thanks in advance guys and apologies for the rambling, just want to make sure I make sense!

  • 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-31T16:20:17+00:00Added an answer on May 31, 2026 at 4:20 pm

    Using devise you can simply add a line to the create action in the controller that devise recognises and actually does this relationship for you without having to create nested attributes or forms. It’s the ‘@post.user = current_user’ line below that automagically does this! So the models are still technically nested if you will but you don’t have to change any of your original forms etc. to get them to nest to users correctly, it just passes the user ID.

    i.e.

    def create
      @post = Post.new(params[:post])
      @post.user = current_user
      respond_to do |format|
        if @post.save
          format.html { redirect_to @post, :notice => 'Post was successfully created.' }
          format.json { render :json => @post, :status => :created, :location => @post }
        else
          format.html { render :action => "new" }
          format.json { render :json => @snippet.errors, :status => :unprocessable_entity }
        end
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is probably a really simple question but... I would like to do a
This is probably a really simple jQuery question, but I couldn't answer it after
This is probably a simple question, but I really don't know what I'm doing
This is probably a really dumb question with a simple answer but... I am
Ok, this probably has a really simple answer, but I've never tried to do
Probably a really simple one this - I'm starting out with C# and need
This is probably a really simple question but I can't seem to find anything
This is probably a really simple question, but I don't write stored procedures often
Ugh, how do I explain this one... Probably a simple question but my mind
I know this is probably a really simple question but I'm having a brain

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.