lundi 30 mai 2016

Rails not assigning proper ids

I have a program with service_provider and service models. The service_provider uses the has_many attribution and the service model uses belongs_to attribution. service_provider was created using devise. I am unable to assign a service_provider_id to the service in the create method.

The create method in services_controller.rb

def create
    @service = Service.new(service_params)
    @service.service_provider_id = current_service_provider.id    
    respond_to do |format|
      if @service.save
          format.html { redirect_to @service, notice: 'Service was     successfully created.' }
          format.json { render :show, status: :created, location: @service }
      else
          format.html { render :new }
          format.json { render json: @service.errors, status: :unprocessable_entity }
      end
    end
end

The rails console gives service_provider_id: nil for every service.

Here's the schema.rb where service_provider_id was created using the migration "t.belongs_to :service_provider, index: true"

create_table "service_providers", 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.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",                          null: false
    t.datetime "updated_at",                          null: false
end

add_index "service_providers", ["email"], name:   "index_service_providers_on_email", unique: true
add_index "service_providers", ["reset_password_token"], name:  "index_service_providers_on_reset_password_token", unique: true
create_table "services", force: :cascade do |t|
    t.integer  "service_provider_id"
    t.string   "service_name"
    t.text     "description"
    t.integer  "price"
    t.string   "location"
    t.datetime "created_at",          null: false
    t.datetime "updated_at",          null: false
end

add_index "services", ["service_provider_id"], name: "index_services_on_service_provider_id"

Here's the service.rb model

class Service < ActiveRecord::Base
   attr_accessor :service_provider_id
   belongs_to :service_provider
   validates_presence_of :description, :location, :service_name, :price
end

And the 'service_provider.rb`

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

I tried restarting the server and resetting the database with no luck.

Aucun commentaire:

Enregistrer un commentaire