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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:10:27+00:00 2026-05-24T03:10:27+00:00

This is a consistency problem that I’m running into often. Let’s consider a typical

  • 0

This is a consistency problem that I’m running into often.

Let’s consider a typical Forum:

  • User can create Posts
  • Posts belong to a Topic
  • Posts also belong to the User that created them

What’s the best practice for choosing between these two options:

# Initialize @post on the User
def create
  @post = current_user.posts.build(params[:post])
  @post.topic_id = @topic.id
  if @post.save
    ...
  end
end

Or

# Initialize @post on the Topic
def create
  @post = @topic.posts.build(params[:post])
  @post.user_id = current_user.id
  if @post.save
    ...
  end
end

Or is there a better way, considering that, in the above examples, either @post’s user_id or topic_id would have to be added to attr_accesssible (feels hacky)?

  • 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-24T03:10:28+00:00Added an answer on May 24, 2026 at 3:10 am

    You never need to monkey with IDs or attr_accessible. If a User has_many Posts and a Topic has_many Posts than you can do

    # Initialize @post on the User
    def create
      @post = current_user.posts.build(params[:post])
      @post.topic = @topic #assuming you've gotten the topic from somewhere
      if @post.save
        ...
      end
    end 
    

    There really isn’t a big difference in building from the user or from the topic, but going from the user seems more natural to me.

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

Sidebar

Related Questions

I have an application that loads lots of data into memory (this is because
This is the reader-writer problem for just read consistency. Here is the algorithm: void
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)] I saw this attribute in the reflector for the base class libraries
I encounter this issue pretty consistently when trying to merge a branch back into
This is a bit of a long shot, but if anyone can figure it
Curious if anyone has this problem: I save my (embedded) fonts in an external
I am trying to extract numbers from a typical scoreboard that you would find
From this post . One obvious problem is scalability/performance. What are the other problems
I have read this post: what happens to NSLog info when running on a
I've worked on projects that have very complex XML configuration, and one problem that's

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.