Home > Net >  rails - can't save data in associated models in RoR
rails - can't save data in associated models in RoR

Time:06-04

I have 2 Models with association has_many along with cascade property between them.

class ServicesBrandDetail < ApplicationRecord
    has_many :services_brands, foreign_key: "brand_id", dependent: :delete_all
end


class ServicesBrand < ApplicationRecord
  belongs_to :services_brand_details, foreign_key: "brand_id", 
end

Migration for both files

class CreateServicesBrandDetails < ActiveRecord::Migration[6.1]
  def change
    create_table :services_brand_details do |t|
      t.string :brand
      t.string :mail_list
      t.string :cc_list

      t.timestamps
    end
  end
end

class CreateServicesBrands < ActiveRecord::Migration[6.1]
  def change
    create_table :services_brands do |t|
      t.string :warehouse
      t.references :brand, null: false, foreign_key: {to_table: :services_brand_details}

      t.timestamps
    end
  end
end

Now I was able to create and save data in from ServicesBrandDetails model. but the Problem is when i create record from ServiceBrand It created record perfectly but i was not able to store data in DB.

record = ServicesBrandDetail.create(:brand => "a", :mail_list => '[email protected]', :cc_list => '[email protected]')
record.save

Record successfully stored in DB.

child = record.services_brands.new(:warehouse => "in") <-- record was created successfully.
child.save

it give me error

C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/activerecord-6.1.5/lib/active_record/inheritance.rb:237:in `compute_type': uninitialized constant ServicesBrand::ServicesBrandDetails (NameError)

CodePudding user response:

In your model ServicesBrand you have to use singular association name for belongs_to

Change this belongs_to :services_brand_details to this belongs_to :services_brand_detail

class ServicesBrand < ApplicationRecord
  belongs_to :services_brand_detail, foreign_key: "brand_id"
end

CodePudding user response:

Please follow proper Naming convention

This article might help - https://www.bigbinary.com/learn-rubyonrails-book/summarizing-rails-naming-conventions

In ServiceBrand Model

class ServiceBrand < ApplicationRecord
  belongs_to :brand, class_name: 'ServiceBrandDetail'
end

belongs_to should be foreign key name i.e brand in your case

You can delete existing models and tables from your codebase and try below one. (I've tested)

class ServiceBrandDetail < ApplicationRecord
  has_many :service_brands, foreign_key: :brand_id, dependent: :delete_all
end


class ServiceBrand < ApplicationRecord
  belongs_to :brand, class_name: 'ServiceBrandDetail'
end


Migration for both files

class CreateServiceBrandDetails < ActiveRecord::Migration[6.1]
  def change
    create_table :service_brand_details do |t|
      t.string :brand
      t.string :mail_list
      t.string :cc_list

      t.timestamps
    end
  end
end

class CreateServiceBrands < ActiveRecord::Migration[6.1]
  def change
    create_table :service_brands do |t|
      t.string :warehouse
      t.references :brand, null: false, foreign_key: {to_table: :service_brand_details}

      t.timestamps
    end
  end
end

Then try to create model objects which you tried in your question. It will work

  • Related