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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:03:34+00:00 2026-06-12T19:03:34+00:00

I have a table in the database called pcomment. Each pcomment belongs_to a purchase.

  • 0

I have a table in the database called pcomment. Each pcomment belongs_to a purchase. A purchase has_many pcomments. additionally, each user has_many pcomments and each pcomment belongs_to user. In the database, the pcomments table has id/user_id/purchase_id/body/created_at/updated_at

Everything is getting filled in correctly except for user_id is still always blank. I want it to be filled in with the user_id of the current signed in user (ie the one who is creating the pcomment.) Both the user_id and pcomment_id are indexed in the table with add_index (during the migration)

Here is the form for the pcomment

  <%= form_for([purchase, purchase.pcomments.build]) do |f| %>

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


    </div>


  <% end %>

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])

    @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

If you need more of my code it is at https://github.com/tradespring1/tradespring/

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

    This probably isn’t the best way to do it, but if you’ve defined a “current_user” method, or are using devise you can simply put @pcomment.user_id = current_user.id right before @pcomment.save

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

Sidebar

Related Questions

I have a table called routes within my database where each route has an
I have a table in database called user which has following entries. Userid FirstName
In database table I have column called options . It has type of integer
I have a table called Products in my database. It has a couple of
I have a table in my database called OrderItemType which has about 5 records
I have a database table called users This table has two columns (that are
I have a database table called posts that has 'id, title, slug and content'
I have a table in my database called Task. Task has the following fields:
Let's say I have a table in my database called Orders that has the
I have a table in my SQL Server database called items which has a

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.