0

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?

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.