Shawn Wilson
Shawn Wilson

Reputation: 1351

Adding multiple users into a single record Rails 5

So I have an app rails 5, ruby 2.4.0.

I have a truck model and a user model.

when a user creates a new Truck, they need to add an owner and a driver (both users)

When this record is created it automatically saves the user who created the record aswell.

right now when I add a new truck the User id is saved and the owner_id and driver_id are also saved.

when I try to list the owner, driver and user names on the record, they over write each other, I am also now getting this error:

Better Errors screenshot

my Truck model:

class Truck < ApplicationRecord
  has_many :users
end

my user model:

class User < ApplicationRecord

  devise :invitable, :database_authenticatable, :recoverable, :rememberable, :trackable, :validatable, :confirmable, :invitable

  has_many :trip_categories
  has_many :trucks

end

my trucks controller (show action):

class TrucksController < ApplicationController

  # GET /trucks/1
  # GET /trucks/1.json
def show
   @truck = Truck.find(params[:id])
   @truck.owner_id = User.find_by(params[:owner_id])
   @truck.driver_id = User.find_by(params[:driver_id])
   @truck.user = User.find_by(params[:user_id])
 end


  private
    # Use callbacks to share common setup or constraints between actions.
    def set_truck
      @truck = Truck.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def truck_params
      params.require(:truck).permit(:make, :model, :color, :vin, :unit_number, :owner_id, :driver_id, :user_id)
    end
end

Its been a long while since I've done anything like this any help here would be greatly appreciated, Please let me know if anything else is needed for help in troubleshooting this problem.

EDIT 1: Add screenshot of new error:

Better errors screenshot

Upvotes: 2

Views: 609

Answers (2)

Kevin.Xin
Kevin.Xin

Reputation: 86

You should add two relationships between user and truck as following

class User
  has_many :own_trucks, as: :owner, class_name: 'Truck'
  has_many :driving_trucks, as: :driver, class_name: 'Truck'
end

class Truck
  belongs_to :owner, class_name: 'User'
  belongs_to :driver, class_name: 'User'
  belongs_to :user, class_name: 'User' <-- added - Edited to provide full answer to solution
end



def create
  # @truck = Truck.new
  # @truck.owner = User.find(params[:owner_id])
  # @truck.driver = User.find(params[:driver_id])
  @truck = Truck.new(params.permit(:driver_id, :owner_id, :xxxx1, :xxxx2))
  @truck.save
end

def show
  @truck = Truck.find(params[:id])
end

Upvotes: 3

qpzm
qpzm

Reputation: 448

has_many :users adds an instance method users to the Truck class.

You can add users like

@truck.users << user1
@truck.users << user2
@truck.users << user3
# or equivalently
@truck.users << [user1, user2, user3]

Regarding EDIT1, you should has_many :users does not add user method, so the error says it is undefined.

Upvotes: 1

Related Questions