Rails Basic ActiveRecord Methods

Claudia Borghini
2 min readAug 13, 2020

ActiveRecord methods described in this article: create, new, assign_attributes, save, update, build, pluck, where, find_by, find

create
Returns a new object and saves it to the database. The new object can be created from a hash, a block, or with attributes manually set after creation.

lucy = Dog.create(name: "Lucy", age: 4, :breed "Dalmatian")

When using a form to get the attributes’ values from the user, to create a new instance you can pass in those attributes using strong parameters, a hash of key, value pairs. Strong parameters permit the user to define just the given attributes, and nothing more.

def strong_params
params.require(:dog).permit(:name, :age, :breed)
end
lucy = Dog.create(strong_params)

new
Initiates a new instance of a class, without assigning any attributes. It returns an empty skeleton, and it doesn’t save it to the database.

snoopy = Dog.new

assign_attributes
It’s used when editing the attributes of an existing record. It assigns the edited attributes to the object, by passing in a hash of keys and value pairs. It doesn’t save the edited object to the database yet.
If using a form to edit the attributes, you can use the strong_params method to assign these attributes to the object.

fido = Dog.assign_attributes(name: "Fido", age: 6, breed: "Labrador")or
fido = Dog.assign_attributes(strong_params)

save
Saves the record to the database, used after new, and assign_attributes.

fido.save

update
Updates the attributes of an object and saves the changes to the database. It requires to pass an argument with the edited values. If you get the edited attributes from the user with an edit form, you can use the strong_params method.

lucy.update(name: "Lucy", age: 5, :breed "Dalmatian")or
lucy.update(strong_params)

build

Similar to new, build initiates a new instance of a class, but it doesn’t save it to the database.
When used with associations, build creates the association between the object that is being built and the other model.
Owner has many dogs
Dog belongs to an owner

shaggy = Owner.new
scooby_doo = shaggy.dogs.build #=> builds a dog instance and shovels it in the collection of dogs of this owner

pluck

Returns a new array, that contains all values of a certain column.

class Dog  def names
pluck(:name)
end
end

what pluck does is:

Dog.all.map do |dog|
dog.name
end

Dog.names => [“Lucy”, “Fido”, “Snoopy”, “Scooby Doo”]

where

Dog.where(breed: "Dalmatian") 
#=> returns all dogs whose breed is Dalmatian

Where returns an array of objects that meet certain search criteria. Where needs an argument, the key is the column name we want to search for, and the value is the values we are searching for.

find_by

Finds the first record matching the search condition. Similar to where, you need to pass an argument with a key(column name) and value that you are searching for.

If no record is found, it returns nil.

Dog.find_by(name: "Lucy")
#=> Returns the first dog instance whose name is Lucy.

find

Needs an argument of an id. Returns the object with that id number.

Dog.find(1)
#=> returns the dog object saved at id 1 in the database.

Thanks for reading, I hope this was helpful!

Happy coding!

--

--