0

i am new to rails so any help would be much appreciated. I have the models users and adverts. User has_many adverts and Advert belongs to a user.

Question: when a user creates an advert i want the created advert to be automatically linked to the current user that created the advert. I am unsure how to go about this

i know how to assign advert to a user in the console

advert = Advert.first
advert.userr_id = 3
advert.save

but i am unsure how to implement this in my controller. Any directional advise would be much appreciated

schema

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

  create_table "adverts", force: true do |t|
    t.string   "title"
    t.text     "content"
    t.integer  "category_jobtype_id"
    t.integer  "category_positiontype_id"
    t.integer  "salarystart"
    t.integer  "salaryend"
    t.integer  "category_country_id"
    t.string   "city"
    t.string   "town"
    t.string   "postcode"
    t.integer  "category_editorialapproval_id"
    t.integer  "category_applicationrequest_id"
    t.datetime "created_at"
    t.datetime "updated_at"
    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   "firstname"
    t.string   "lastname"
    t.string   "companyname"
    t.integer  "category_businesstype_id"
  end

  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

models

class Advert < ActiveRecord::Base
  belongs_to :user
end

class Userr < ActiveRecord::Base
  has_many :adverts
end

controller: Advert

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

  def index
    @adverts = Advert.all
    respond_with(@adverts)
  end

  def show
    respond_with(@advert)
  end

  def new
    @advert = Advert.new
    respond_with(@advert)
  end

  def edit
  end

  def create
    @advert = Advert.new(advert_params)
    @advert.save
    respond_with(@advert)
  end

  def update
    @advert.update(advert_params)
    respond_with(@advert)
  end

  def destroy
    @advert.destroy
    respond_with(@advert)
  end

  private
    def set_advert
      @advert = Advert.find(params[:id])
    end

    def advert_params
      params.require(:advert).permit(:title, :content, :category_jobtype_id, :category_positiontype_id, :salarystart, :salaryend, :category_country_id, :city, :town, :postcode, :category_editorialapproval_id, :category_applicationrequest_id)
    end
end

controller: Userr

any help will be much appreciated. A book recommendation or a tutorial will be helpful....many thanks

1 Answer 1

0

Once you have the user object accessible, basically you can just do:

def create
  @advert = Advert.create(advert_params)
  user.adverts << @advert
  respond_with(@advert)
end

When you use the has_many method, the model receives some methods "for free". The <<() method is just one of them. You can find out many things about associations and these methods automatically generated here.

1
  • thanks @Tiago but it doesn't seem to work. when a user creates an advert it is not assigning the user to the advert. the Advert still has a user_id in the console stated as 'nil'
    – ARTLoe
    Apr 2, 2015 at 10:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.