假設我有一個紅寶石哈希:保存使用Ruby
person={:name=>:Alex,:age=>10}
我希望我可以這樣調用的API:
db.save :people, person
,這將在執行下面的SQL mysql數據庫:
insert nto people (name,age) values ('Alex',10)
這是可能的通過使用導軌或其他紅寶石?
假設我有一個紅寶石哈希:保存使用Ruby
person={:name=>:Alex,:age=>10}
我希望我可以這樣調用的API:
db.save :people, person
,這將在執行下面的SQL mysql數據庫:
insert nto people (name,age) values ('Alex',10)
這是可能的通過使用導軌或其他紅寶石?
你可以叫ClassName.create(your_hash),所以在您的方案會是這樣的:
Person.create!(person) # to create a record right away
或
person = Person.new(person) # to initialize the object first
person.save # and then save it
無需使用任何外部的寶石,這是標準ActiveRecord,Rails核心庫之一。
類Person <的ActiveRecord :: Base的 結束在模型文件
放Person類。相應的表名應該是人,其複數型號名和首字母將是小寫。只需看看Rails的慣例。
現在嘗試
Person.create!(attributes).
屬性是一樣的列名對應的值。
例如
{:name => 'sandip'} #here name is the column name and value is the 'sandip'