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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:28:26+00:00 2026-06-12T17:28:26+00:00

I have a field that creates a comment (named pcomment). I am trying to

  • 0

I have a field that creates a comment (named pcomment). I am trying to get it to automatically add the user_id to the pcomment in the pcomment table like it adds the purchase_id automatically. I am not sure why the purchase_id is being recorded in the database but the user_id remains blank for each pcomment. Here is the form for the pcomment.

<%= form_for([purchase, purchase.pcomments.build], :html => { :id => "blah_form" }) do |f| %>

    <div class="field">
      <h4>What deal are you offering?</h4>
      <%= f.text_field :body %>
    </div>

  <% end %>

It may be that I have to add some hidden_field, but I don’t think so. I am using http://ruby.railstutorial.org/book/ruby-on-rails-tutorial#cha-user_microposts as resource and in that the microposts dont have any hidden_field. Instead, the user_id is indexed and it automatically is created upon the creation of a micropost (based on who is signed in at the time). This part is working for me too, adding to my rational that indexing user_id on the pcomments table is enough to automatically generate it. Here is my schema.rb file so that you can see the current state of my database.

   ActiveRecord::Schema.define(:version => 20121011085147) do

  create_table "pcomments", :force => true do |t|
    t.string   "body"
    t.integer  "purchase_id"
    t.integer  "user_id"
    t.datetime "created_at",  :null => false
    t.datetime "updated_at",  :null => false
  end

  add_index "pcomments", ["purchase_id"], :name => "index_pcomments_on_purchase_id"
  add_index "pcomments", ["user_id"], :name => "index_pcomments_on_user_id"

  create_table "purchases", :force => true do |t|
    t.string   "content"
    t.integer  "user_id"
    t.datetime "created_at", :null => false
    t.datetime "updated_at", :null => false
  end

  add_index "purchases", ["user_id", "created_at"], :name => "index_purchases_on_user_id_and_created_at"

  create_table "sales", :force => true do |t|
    t.string   "content"
    t.integer  "user_id"
    t.datetime "created_at", :null => false
    t.datetime "updated_at", :null => false
  end

  add_index "sales", ["user_id", "created_at"], :name => "index_sales_on_user_id_and_created_at"

  create_table "scomments", :force => true do |t|
    t.string   "body"
    t.integer  "sale_id"
    t.integer  "user_id"
    t.datetime "created_at", :null => false
    t.datetime "updated_at", :null => false
  end

  add_index "scomments", ["sale_id"], :name => "index_scomments_on_sale_id"
  add_index "scomments", ["user_id"], :name => "index_scomments_on_user_id"

  create_table "users", :force => true do |t|
    t.string   "name"
    t.string   "email"
    t.datetime "created_at",                         :null => false
    t.datetime "updated_at",                         :null => false
    t.string   "password_digest"
    t.string   "remember_token"
    t.boolean  "admin",           :default => false
  end

  add_index "users", ["email"], :name => "index_users_on_email", :unique => true
  add_index "users", ["remember_token"], :name => "index_users_on_remember_token"

end

and the reason I know its not working is that I check in the database and the pcomment is successfully created with all columns filled in including purchase_id but the user_id is still blank. also, the user has_many pcomments and has_many purchases. The purchase has_many pcomments and belongs_to user. The pcomment belongs_to user and belong_to purchase.

also, here is the pcomments_controller.rb

    class PcommentsController < ApplicationController
  before_filter :signed_in_user
  def create
    @purchase = Purchase.find(params[:purchase_id])
    @pcomment = @purchase.pcomments.build(params[:pcomment], :user_id => @purchase.user_id)

    @pcomment.purchase = @purchase

    if @pcomment.save
       flash[:success] = "Offer submited!"
       redirect_to :back
    else
      render 'shared/_pcomment_form'
    end
  end

  def new
    @pcomment=purchase.pcomments.new
  end


end

      def new
        @pcomment=purchase.pcomments.new(:user_id => purchase.user_id)
      end


    end
  • 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-12T17:28:28+00:00Added an answer on June 12, 2026 at 5:28 pm

    purchase.pcomments.build builds empty Pcomment object just with purchase_id filled from purchase. To assign also user_id pass the hash with attribute:

    purchase.pcomments.build(:user_id => purchase.user_id)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a field on a from that a user creates a return code.
We have created a table with a trigger that updates a ModifiedDate field in
I have field that it's not a key (it's text) and I want to
I have field X that contains text with spaces in the end of the
I have a field that indicates whether someone wars a commission or a fee.
I have a field that is in this format 5551112391^HUMAN^HUMAN-800-800^6-main^^ How would I only
I have a field that may be blank, so I need to use Object.try,
I'm going to be importing the data and I have a field that will
I have a data field that contains large numbers in two formats: 553000.468...705.46.0000000 <-
I have a user control named Navigation that has a property that holds navigation

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.