ARTLoe ARTLoe - 4 months ago 14
Ruby Question

Rails Associations - user_id states nil Rails 4

I am completely unsure where i am going wrong and it surprises me that i can not figure it out, so any advise would be much appreciated.


  1. i have 2 models userr(recruiters) & feedbackr(recruiters feedback)

  2. [userr model]
    userr has_many :feedbackr

  3. [feedbackr model]
    belongs_to :userr

  4. [schema] i have added
    userr_id
    column to the table feedbackrs

  5. [feedbackrs_controller] i have added
    :userr_id
    to the
    feedbackr_params




the problem is, when i create a feedback comment as a recruiter & go
into my console and type
Feebackr.find(4)
(the last created
feedback) the
userr_id
shows
nil
- it
is suppose to display the
id
of the userr(recruiter) that created a
feedback - i am unsure why it is displaying
nil
as my associations all seem right -


any advise would be much appreciated - i have my files below -

console

2.1.2 :055 > ap Feedbackr.find(4)
Feedbackr Load (0.3ms) SELECT "feedbackrs".* FROM "feedbackrs" WHERE "feedbackrs"."id" = ? LIMIT 1 [["id", 4]]
#<Feedbackr:0x007fd89136e018> {
:id => 4,
:email => "richill@gmail.com",
:created_at => Sun, 28 Jun 2015 00:29:52 UTC +00:00,
:updated_at => Sun, 28 Jun 2015 00:29:52 UTC +00:00,
:category_feedbackr_id => 6,
:content => "feedback1",
:userr_id => nil
}
=> nil
2.1.2 :056 >


schema

ActiveRecord::Schema.define(version: 20150627235330) do

create_table "feedbackrs", force: true do |t|
t.string "email"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "category_feedbackr_id"
t.text "content"
t.integer "userr_id"
end

create_table "userrs", force: true do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at"
t.datetime "updated_at"
t.string "confirmation_token"
t.datetime "confirmed_at"
t.datetime "confirmation_sent_at"
t.string "unconfirmed_email"
end

add_index "userrs", ["confirmation_token"], name: "index_userrs_on_confirmation_token", unique: true
add_index "userrs", ["email"], name: "index_userrs_on_email", unique: true
add_index "userrs", ["reset_password_token"], name: "index_userrs_on_reset_password_token", unique: true

end


feedbackr.rb

class Feedbackr < ActiveRecord::Base
belongs_to :userr
end


userr.rb

class Userr < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :confirmable

has_many :feedbackrs
end


feedbackr_controller.rb

class FeedbackrsController < ApplicationController
respond_to :html, :xml, :json
before_action :set_feedbackr, only: [:show, :edit, :update, :destroy]

def index
@feedbackrs = Feedbackr.all.order("created_at DESC")
respond_with(@feedbackrs)
end

def show
respond_with(@feedbackr)
end

def new
@feedbackr = Feedbackr.new
respond_with(@feedbackr)
end

def edit
end

def create
@feedbackr = Feedbackr.new(feedbackr_params)
respond_to do |format|
if @feedbackr.save
format.html { redirect_to dashboard_path, :notice => 'Thank you for your feedback' }
format.xml { render :xml => @feedbackr, :status => :created, :location => [@feedbackr] }
else
format.html { render :action => "new" }
format.xml { render :xml => @feedbackr.errors, :status => :unprocessable_entity }
end
end
end

def update
...
end

def destroy
...
end

private
def set_feedbackr
@feedbackr = Feedbackr.find(params[:id])
end

def feedbackr_params
params.require(:feedbackr).permit(:email, :category_feedbackr_id, :content, :userr_id)
end
end


views/feedbackrs/_form.html.erb

<%= simple_form_for(@feedbackr) do |f| %>
<%= f.error_notification %>

<div class="form-inputs">
<%= f.input_field :email, label: 'email address', value: current_userr.email %>
<%= f.association :category_feedbackr, as: :radio_buttons, label: 'How likely are you to recommend us?' %>
<%= f.input :content, label: 'What is the primary reason for your score?'%>
</div>

<div class="form-actions">
<%= f.button :submit, 'Provide Feedback' %>
</div>
<% end %>
<div><%= render 'shared/footer' %></div>

Answer

Did you want to set the current_user as the userr ?

def create
    @feedbackr = Feedbackr.new(feedbackr_params)
    @feedbackr.userr = current_user # And make sure the user is authenticated (though you probably did that in your ApplicationController ?)
    respond_to do |format|
      if @feedbackr.save
        format.html { redirect_to dashboard_path, :notice => 'Thank you for your feedback' }
        format.xml  { render :xml => @feedbackr, :status => :created, :location => [@feedbackr] }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @feedbackr.errors, :status => :unprocessable_entity }
      end  
    end
  end