appleLover
appleLover

Reputation: 15691

Ruby Rails - Create Entry, Hash Has Extraneous Keys

A rails table Book which just has one column named title.

Book.create( { title: "A Gay Science", rating: 10 })
`rescue in _assign_attribute': unknown attribute 'rating'

This is a simplified example... Right now I loop through a list of such hashes which have extraneous keys and either delete the offenders or generate a new list of fresh hashes to insert into the database. Is there a better way, maybe a method already designed to deal with this issue?

Upvotes: 0

Views: 28

Answers (1)

Padhu
Padhu

Reputation: 1580

If you have the list of array like below,

array = [{ title: "A Gay Science", rating: 10 }, { title: "A Gay Science", rating: 10 }, { title: "A Gay Science", rating: 10 }]

You can do something like this,

array.each do |a| 
Book.create(title: a[:title])
end

Upvotes: 1

Related Questions