Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby ActiveRecord Basics Callbacks Callbacks and Relationships

:When to use a colon:

During Hampton's video on Callbacks, near the end of the video, he shows two examples that have me confused. has_many :account_entries, dependent: :destroy and belongs_to :account , :dependent => :destroy

Can someone explain why a colon is used at the end of 'dependent' in the first example yet at the beginning of the second example?

:Thanks:

Chris

1 Answer

Benjamin Simmons
Benjamin Simmons
50,431 Points

Both are Symbols in Ruby, but there are two different ways to do it. The first way is

:dependent => :destroy

In this example, both :dependent and :destroy are symbols, and you are setting :dependent to the value of :destroy.

In newer versions of ruby, you do not have to use the rocket syntax (=>), but can use a colon at the end of the symbol that you are setting the value of, like in Javascript.

dependent: :destroy

Both are valid syntax, and sometimes Ruby developers that have been doing Ruby for a long time are just in the habit of the rocket syntax. However, as you get into real world projects, there are very few situations that still require the rocket syntax. I normally use the newer syntax, but it's important to know both

Thank you for adding this. Totally confused by that for months and never know which one to go with in ERB link_to tags etc.