Created
September 10, 2014 19:41
-
-
Save probablykabari/325363af95fde9b034be to your computer and use it in GitHub Desktop.
just showing some syntax stuff
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# I see this a lot, and it is perfectly OK to do this | |
def new_cat(name, color) | |
cat = Cat.new | |
cat.name = name | |
cat.owner = self | |
cat.color = color | |
cat.save | |
end | |
# Here's another way to do that | |
# This does the same thing but doesn't need a local var | |
# returns true/false | |
def newer_cat(name, color) | |
Cat.create do |cat| | |
cat.name = name | |
cat.owner = self | |
cat.color = color | |
end | |
end | |
# or make a bunch of them | |
# *note that this will fail on mass assignment unless you specify to ignore it | |
def create_cat_lady(*attrs) | |
Cat.create(attrs) do |cat| | |
cat.owner = self | |
end | |
end | |
# creates two cats, returns true/false | |
create_cat_lady({name: 'meowmeow', color: 'brown'}, {name: 'curtis', color: 'orange'}) | |
# And lastly, if you don't need/want to save the object yet | |
def new_cat_instance(name, color) | |
Cat.new do |cat| | |
cat.name = name | |
cat.owner = self | |
cat.color = agent_uuid | |
end | |
end | |
# this returns the instance, so you don't need to create a var inside the method | |
my_new_cat = new_cat_instance('mittens', 'brown and white') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment