{"id":2786,"date":"2012-05-28T13:00:36","date_gmt":"2012-05-28T16:00:36","guid":{"rendered":"http:\/\/blog.plataformatec.com.br\/?p=2786"},"modified":"2012-05-28T13:38:23","modified_gmt":"2012-05-28T16:38:23","slug":"active-record-attribute-method","status":"publish","type":"post","link":"https:\/\/blog.plataformatec.com.br\/2012\/05\/active-record-attribute-method\/","title":{"rendered":"Active Record `attribute?` method"},"content":{"rendered":"
Last week, while developing a feature I had some code like this in a view:<\/p>\n
link_to_unless @post.url.blank?, \"Link to post\", @post.url<\/pre>\nWhere url is an attribute of type String<\/code>. As people usually don’t like negative conditionals, we can easily rewrite this code as:<\/p>\nlink_to_if @post.url.present?, \"Link to post\", @post.url<\/pre>\nHowever, we can clean our code a little bit using the #{attribute}?<\/code> method.<\/p>\nlink_to_if @post.url?, \"Link to post\", @post.url<\/pre>\nThe #{attribute}?<\/code> method is a method defined automatically by Active Record. Intuitively, you may think that it is equivalent to !!url<\/code> and then would evaluate to true when an empty string, but Rails is smart enough to detect that the attribute is a string and returns the equivalent of !url.blank?<\/code>, as you may see in these lines in Rails source code<\/a>, which proved itself very useful.<\/p>\nActive Record defines many other attribute methods like that, for example url_changed?<\/code>. What about you? What is your favorite Rails attribute method?<\/p>\n","protected":false},"excerpt":{"rendered":"Last week, while developing a feature I had some code like this in a view: link_to_unless @post.url.blank?, “Link to post”, @post.url Where url is an attribute of type String. As people usually don’t like negative conditionals, we can easily rewrite this code as: link_to_if @post.url.present?, “Link to post”, @post.url However, we can clean our code … \u00bb<\/a><\/p>\n","protected":false},"author":12,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0,"footnotes":""},"categories":[1],"tags":[],"aioseo_notices":[],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/posts\/2786"}],"collection":[{"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/users\/12"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/comments?post=2786"}],"version-history":[{"count":15,"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/posts\/2786\/revisions"}],"predecessor-version":[{"id":2802,"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/posts\/2786\/revisions\/2802"}],"wp:attachment":[{"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/media?parent=2786"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/categories?post=2786"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/tags?post=2786"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}