This question is a bit complicated to ask and probably more difficult to understand so I will do my best to add context to explain my goal.

My app allows logged-in users to run a web scraping function on a car dealership's website and stores it's inventory to a database. I would like to associate the current\_user.id of who ran the scraping function to the posts created by it.

First, I generated a migration to associate "vehicles" with "users" by running

> 
> `rails g migration AddUserRefToVehicles user:references`
> 
> 
> 

then, I added a belongs\_to :user association to the Vehicle model

then, I updated the create action in VehiclesController

The VehiclesController calls on a "scrape" function which runs the "vehicles\_spider.rb" model and creates the posts and saves it to a database.

**vehicles\_controller.rb :**

```
    class VehiclesController < ApplicationController
      before_action :authenticate_user!
      before_action :set_vehicle, only: %i[ show edit update destroy ]

      ...

      #Calls vehicles_spider.rb
      def scrape
        url = 'https://www.example.com/vehicles'
        response = VehiclesSpider.process(url)
        if response[:status] == :completed && response[:error].nil?
          flash.now[:notice] = "Successfully scraped url"
        else
          flash.now[:alert] = response[:error]
        end
      rescue StandardError => e
      flash.now[:alert] = "Error: #{e}"
      end

      def new
        @vehicle = Vehicle.new
      end

      def create
                   #This didn't work
        @vehicle = Vehicle.new(vehicle_params.merge(user_id: current_user.id))
      end

...

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

        # Only allow a list of trusted parameters through.
        def vehicle_params
          params.require(:vehicle).permit(:title, :stock_number, :exterior_color, 
              :interior_color, :transmission, :drivetrain, :price)
        end
    end

```

**vehicle.rb**

```
class Vehicle < ApplicationRecord
    belongs_to :user
end

```

**vehicles\_spider.rb**

```
class VehiclesSpider < Kimurai::Base

require 'nokogiri'
require 'httparty'
require 'byebug'
require 'watir'

  @name = 'vehicles_spider'
  @engine = :mechanize

  def self.process(url)
    @start_urls = [url]
    self.crawl!
  end

  def parse(response, url:, data: {})
    url = "https://www.example.com/new-vehicles/"

    { ... }

            pagination_vehicle_listings.each do |vehicle_listing|
                vehicle = {title: ...
                           price: ...
                           color: ...          
                                      }
                // This line is what creates a post and saves the scraped data to my database.
                  Vehicle.where(vehicle).first_or_create

                   { ... }
     end

```

**schema.rb**

```
ActiveRecord::Schema.define(version: 2021_07_14_104441) do

  # These are extensions that must be enabled in order to support this database
  enable_extension "plpgsql"

  create_table "users", force: :cascade 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.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.string "name"
    t.index ["email"], name: "index_users_on_email", unique: true
    t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
  end

  create_table "vehicles", force: :cascade do |t|
    t.string "title"
    t.string "stock_number"
    t.string "exterior_color"
    t.string "interior_color"
    t.string "transmission"
    t.string "drivetrain"
    t.integer "price"
    t.integer "miles"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.bigint "user_id", null: false
    t.index ["user_id"], name: "index_vehicles_on_user_id"
  end

  add_foreign_key "vehicles", "users"
end

```

The program was saving the posts just fine until I associated "vehicles" to "users" and now the scraped data is refusing to save. My hunch is that the vehicle posts that are being created by the "vehicle\_spider.rb" model arn't being associated with the logged-in user.