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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:49:59+00:00 2026-06-14T19:49:59+00:00

I need some help creating a very simple forum in a existing model. What

  • 0

I need some help creating a very simple forum in a existing model.

What I want in a Game page, have a mini forum, where is possible create some topics and some comments to this topics. In the beginning I’m only implement topics.

This is the error I have:

Mysql2::Error: Column 'user_id' cannot be null: INSERT INTO `topics` (`game_id`, `question`, `user_id`) VALUES (1, 'asd', NULL) 

This is my main model:

game.rb

class Game < ActiveRecord::Base
   attr_accessible :name

   validates :user_id, presence: true
   validates :name, presence: true, length: { maximum: 50 }

   belongs_to :user

   has_many :topics, dependent: :destroy

end

topic.rb

class Topic < ActiveRecord::Base
     validates_presence_of :question
     validates_presence_of :game_id

     attr_accessible :question, :user_id

     validates :question, length: {maximum: 50}, allow_blank: false

     belongs_to :game
     belongs_to :user
end

topic_controller.rb

def create
    @game = Game.find(params[:game_id])
    @topic = @game.topics.create(params[:topic])
    @topic.user_id = current_user.id

    respond_to do |format|
      if @topic.save
        format.html { redirect_to @game, notice: 'Topic was successfully created.' }
      else
        format.html { render action: "new" }
      end
    end
end

game/show.html.erb

<h2>Topics</h2>
<% @game.topics.each do |topic| %>

    <p>
      <b>Question:</b>
      <%= topic.question %>
    </p>
<% end %>

<h2>Add a topic:</h2>
<%= form_for([@game, @game.topics.build]) do |f| %>
    <div class="field">
     <%= f.label :question %><br />
      <%= f.text_field :question %>
    </div>
    <div class="actions">
      <%= f.submit %>
    </div>
<% end %>

Thanks 😉

  • 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-14T19:50:00+00:00Added an answer on June 14, 2026 at 7:50 pm

    I believe the issue you’re experiencing is the difference between create and new in Rails.

    Using new merely initializes the model, allowing you to save/validate later; using create will perform all of those steps in one command, causing the database row to be created.

    So when you attempt to perform

    @game.topics.create(params[:topic])
    

    Rails attempts to create a Topic using params[:topic] and setting the game_id to @game.id, after which it immediately attempts to validate this new Topic that it created and save it to the database.

    Potential options that you can consider:

    1) Use @game.topics.new(params[:topic])

    2) merge {:user_id => current_user.id} in: @game.topics.create(params[:topic].merge({:user_id => current_user.id})

    I’d personally suggest option 1 (i.e. using new instead), but I have seen option 2 used before.

    EDIT: Another issue that it looks as though you might be experiencing: should current_user be @current_user in your code?


    Side note:
    generally, if create fails to create database row, it will still work (returning the initialized model instead), but in your case it looks like this won’t happen due to database-level restrictions on user_id being NOT NULL, causing an uncaught error.

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

Sidebar

Related Questions

So I'm creating a very basic python game and I need some help with
Need some help from javascript gurus. I have one page where http://www.google.com/finance/converter is embedded
I need some help with rally. I'm currently looking at creating a simple app
I need some help creating the necessary Javascript to add values to an existing
I'm new in rails and need some help creating a Nested form. I have
I need some help creating a query for my mySQL database. I have recently
i need some help with creating file Im trying in the last hours to
Need some help... I have jasperserver 4.1 installed on my ubuntu. It runs via
Need some help with Activerecord Querying in a has_many :through association. Model: Job class
Need some help, please. I have a line of horizontal thumbnails loaded as ONE

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.