Skip to content

Instantly share code, notes, and snippets.

@thomasmarren
Created October 17, 2016 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thomasmarren/f9f359934bbe2c4610642d4d169d8dc5 to your computer and use it in GitHub Desktop.
Save thomasmarren/f9f359934bbe2c4610642d4d169d8dc5 to your computer and use it in GitHub Desktop.
#Ruby
def self.new_from_db(row)
new_student = self.new(row[1],row[2])
new_student.id = row[0]
new_student
end
def self.find_by_name(name)
sql = "SELECT * FROM users WHERE name = ?"
DB[:conn].execute(sql,name).map do |row|
self.new_from_db(row)
end.first
end
def update
sql = "UPDATE users SET name = ?, age = ? WHERE id = ?"
DB[:conn].execute(sql, self.name, self.age, self.id)
end
tom = user.find_by_name("Tom")
tom.name = "Tommy"
tom.update
#Active Record
user = User.find_by(name: 'David')
user.update(name: 'Dave')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment