Categories

See More
Popular Forum

MBA (4887) B.Tech (1769) Engineering (1486) Class 12 (1030) Study Abroad (1004) Computer Science and Engineering (988) Business Management Studies (865) BBA (846) Diploma (746) CAT (651) B.Com (648) B.Sc (643) JEE Mains (618) Mechanical Engineering (574) Exam (525) India (462) Career (452) All Time Q&A (439) Mass Communication (427) BCA (417) Science (384) Computers & IT (Non-Engg) (383) Medicine & Health Sciences (381) Hotel Management (373) Civil Engineering (353) MCA (349) Tuteehub Top Questions (348) Distance (340) Colleges in India (334)
See More

ActiveRecord::RecordInvalid: Validation failed: Technologies portfolio must exist

General Tech Technology & Software
Max. 2000 characters
Replies

usr_profile.png
Atul Kasana

User

( 3 months ago )

 

I was trying to save nested attributes for technologies with portfolio, I have the following code: portfolio.rb (model)

class Portfolio < ApplicationRecord
  has_many :technologies
  # do not accept the insertion if name is blank
  accepts_nested_attributes_for :technologies,
                                 reject_if: lambda{ |attrs| attrs['name'].blank? }
  validates_presence_of :title, :body, :main_image, :thumb_image
  include Placeholder
  validates_presence_of :title, :body, :main_image, :thumb_image

  # class method - custom scope
  def self.angulars
    where(subtitle: "Angular")
  end 

  # lambda - custom scope
  scope :ruby_on_rails_p_items, -> { where(subtitle: "Ruby on Rails") }

  # callbackss
  after_initialize :set_defaults

  def set_defaults
    self.main_image ||= Placeholder.image_generator(height:'600', width:'400')
    self.thumb_image ||= Placeholder.image_generator(height:'350', width:'200')
  end 
end

technology.rb model:

class Technology < ApplicationRecord
  belongs_to :portfolio
end

Now when I go to the rails console and try to insert multiple attributes:

Portfolio.create!(title: "Web App", subtitle: "asadasd", body: "sadsadas", technologies_attributes: [{name: "Ruby"}, {name: "Rails"}, {name: "Angula"}, {name: "Ionic"}])

It gave me this error:

ActiveRecord::RecordInvalid: Validation failed: Technologies portfolio must exist
    from /Users/mac/.rvm/gems/ruby-2.3.1/gems/activerecord-5.0.7/lib/active_record/validations.rb:78:in `raise_validation_error'

Any idea what am I missing here?

Schema for both technology and portfolio:

  create_table "portfolios",
			
        

what's your interest