Welcome to Giant Robots Smashing Into Other Giant Robots — a weblog about development, business, design and technology — written by thoughtbot.

i did not know that

Here’s something I discovered the other day:

ActiveRecord infers model attributes from the database. Each attribute gets a getter, setter, and query method.

A query method? I know this is true for boolean columns but non boolean columns?

1
2
class User < ActiveRecord::Base
end
  users (id, name)
1
2
3
4
5
6
7
8
9
10
  def test_should_say_if_it_does_or_does_not_have_a_name_when_sent_name?
    user = User.new
    assert ! user.name?

    user.name = ''
    assert ! user.name?

    user.name = 'name'
    assert user.name?
  end

You get that query method for free for each of your ActiveRecord attributes. From this passing test it looks as if for strings its implemented in terms of String#blank?

This is nice because it can save you an extra message.

1
2
  if user.name.blank?
  end

becomes

1
2
  if ! user.name?
  end  

About this entry

 

thoughtbot is hiring

We are hiring web developers and web designers in both Boston and New York, NY.

What are we up to?

We built Shoulda, an eclectic set of additions to Test::Unit; Paperclip to manage uploaded files without hassle; Jester, a REST/ActiveResource client library written in Javascript, and Squirrel, an enhancement for ActiveRecord's find syntax; — amongst some other projects.


Chad (President) and Jon (CTO) co-authored a technical book titled Pro Active Record: Databases with Ruby and Rails, which explores the ins and outs of the ActiveRecord ruby library. You can buy it today at Amazon.com.

About thoughtbot, inc.

We are a small web application development consulting business, with offices in Boston, MA and New York, NY. If you're looking to find a team for your next web development project or your new web application — get in touch.