Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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.