Reputation: 2527
Suppose I have a ruby hash :
person={:name=>:Alex,:age=>10}
I wish I could call an API like this:
db.save :people, person
which will execute the following SQL on mysql database:
insert nto people (name,age) values ('Alex',10)
Is this possible by using rails or other ruby gems?
Upvotes: 0
Views: 325
Reputation: 921
class Person < Activerecord::Base end
put Person class in a models file. corresponding table name should be persons, Its a plural of model name and first letter will be lower case. Just see the convention for Rails.
Now try
Person.create!(attributes).
attributes are like column name corresponding value.
example
{:name => 'sandip'} #here name is the column name and value is the 'sandip'
Upvotes: 0
Reputation: 1191
You can call ClassName.create!(your_hash), so in your scenario it would look like this:
Person.create!(person) # to create a record right away
or
person = Person.new(person) # to initialize the object first
person.save # and then save it
No need to use any external gems, this is standard ActiveRecord, one of the Rails core libraries.
Upvotes: 1