Justin Weiss's Blog, page 2

September 1, 2015

Should you use scopes or class methods?

Scopes are a great way to grab the right objects out of your database:

app/models/review.rb1 2 3 class Review < ActiveRecord::Base scope :most_recent, - (limit) { order("created_at desc").limit(limit) } end

You’d use the scope like this:

app/models/homepage_controller.rb1 @recent_reviews = Review.most_recent(5)

Calling that scope, though, looks exactly like calling a class method on Review. And it’s easy to build it as a class method, instead:

app/models/review.rb1 2 3 def self.most_recent(...<
 •  0 comments  •  flag
Share on Twitter
Published on September 01, 2015 00:30

July 28, 2015

How to predict the future of programming

Why did Rails become so popular, so quickly?

The simplicity helped, especially if you came from the Java, XML, Enterprise world. It was also marketed incredibly well. But that’s not everything.

A lot of Rails’ success in the startup world came from a simple fact: The problems businesses have aren’t that unique. Rails was great at creating CRUD sites, while remaining flexible. And that’s really all a ton of businesses need. Especially at the beginning.

But this isn’t just true for businesses....

 •  0 comments  •  flag
Share on Twitter
Published on July 28, 2015 22:52

July 20, 2015

Turn Ruby conference videos into your own personal podcast

Ruby conferences are awesome. There are so many people sharing so much knowledge, and you’ll take something away from almost every talk. And even if you can’t be there, new conferences mean lots of new talk videos.

But there’s a problem. Videos take time. Even at 1.5x, they’ll still last 20 or 30 minutes each. And that’s focused time that’s hard to find as often as I’d like.

Podcasts, though, have already found a place in my life. I start almost every commute by firing up Overcast and listeni...

 •  0 comments  •  flag
Share on Twitter
Published on July 20, 2015 23:58

July 15, 2015

A web server vs. an app server

When you research how to deploy your Rails app, you’ll see a lot of names: Apache, Unicorn, Puma, Phusion Passenger, Nginx, Rainbows, and many more. They all seem to fit under the “deploying Rails” category of software, but there’s a key difference between them. Some are “web servers,” and others are “app servers.”

Once you understand which is which, and where each category fits in your system, deployment will make a lot more sense. But the categories aren’t always clear.

What’s a web server,...

 •  0 comments  •  flag
Share on Twitter
Published on July 15, 2015 00:13

July 7, 2015

The easiest way to get into open source

Thom Parkin made a great point in the comments of an earlier article of mine:

Great advice. But you missed one very important [final] point. Since this is Open Source, once you have figured out the details of that feature/function where the documentation is a bit light, YOU SHOULD UPDATE THE DOCS AND SUBMIT A PULL REQUEST. In that way the entire community benefits, and you can even gain some “coder cred” for your participation!

I’m happy Thom mentioned this, because it’s so important. Fixing...

 •  0 comments  •  flag
Share on Twitter
Published on July 07, 2015 20:23

July 3, 2015

Creating easy, readable attributes with ActiveRecord enums

After you pick up the Rails basics, you still have a lot to learn. You have to understand gems, DSLs, refactoring, testing, and the deeper parts of Rails itself.

With the Ruby Book Bundle, launching on Monday, July 6, you can kickstart your Rails education with Practicing Rails and 5 other great Ruby books at a huge discount. Sign up to get a reminder when the bundle sale starts!

Imagine a question that can be either “pending”, “approved”, or “flagged”. Or a phone number that’s a “home”, “of...

 •  0 comments  •  flag
Share on Twitter
Published on July 03, 2015 08:31

June 24, 2015

How to preload Rails scopes

Rails’ scopes make it easy to find the records you want:

app/models/review.rb1 2 3 4 5 class Review < ActiveRecord::Base belongs_to :restaurant scope :positive, - { where("rating 3.0") } end 1 2 3 4 irb(main):001:0 Restaurant.first.reviews.positive.count Restaurant Load (0.4ms) SELECT `restaurants`.* FROM `restaurants` ORDER BY `restaurants`.`id` ASC LIMIT 1 (0.6ms) SELECT COUNT(*) FROM `reviews` WHERE `reviews`.`restaurant_id` = 1 AND (rating 3.0) = 5

But if you’re not careful with t...

<
 •  0 comments  •  flag
Share on Twitter
Published on June 24, 2015 08:57

June 23, 2015

How to preload Rails scopes

This article is also available in Korean, thanks to Soonsang Hong!

Rails’ scopes make it easy to find the records you want:

app/models/review.rb1 2 3 4 5 class Review < ActiveRecord::Base belongs_to :restaurant scope :positive, - { where("rating 3.0") } end 1 2 3 4 irb(main):001:0 Restaurant.first.reviews.positive.count Restaurant Load (0.4ms) SELECT `restaurants`.* FROM `restaurants` ORDER BY `restaurants`.`id` ASC LIMIT 1 (0.6ms) SELECT COUNT(*) FROM `reviews` WHERE `reviews`.`restaur...<
 •  0 comments  •  flag
Share on Twitter
Published on June 23, 2015 13:17

June 16, 2015

A guide to the best beginning Rails resources

There are a ton of books, videos, podcasts, and courses for learning Rails. There’s no way you’d have time to go through them all! So what’s the best way for an absolute beginner to learn Ruby and Rails? Which resources should you start with, and when?

Books and websites

If you’re totally new to programming, the best place to start is Learn to Program, by Chris Pine. It’s an intro to the core programming ideas you’ll need to know. If you’re planning to learn Ruby and Rails, it’s especially gr...

 •  0 comments  •  flag
Share on Twitter
Published on June 16, 2015 08:17

June 13, 2015

You've got the Rails basics. So why do you feel so slow?

You’re confident about the core ideas behind Rails. You can write working code, no problem. And you’re learning more about code quality, refactoring, writing great tests, and object-oriented design.

By this point, you’re starting to feel like you’re getting it, that you’re on the path to becoming an expert. When you look backwards, you see just how far you’ve come, and you’re pretty happy with your progress.

So why do you feel so slow? Now that you care about testing, maintainability, and des...

 •  0 comments  •  flag
Share on Twitter
Published on June 13, 2015 19:04