Tag Archives: activerecord

Rails 2.1: Increment and Decrement Value

Prior to Rails 2.1, the increment and decrement functions needed to be called multiple times if you wanted to increment/decrement by more than one. In Rails 2.1, you can pass in a second parameter: a value, how much to increment or decrement the field by. (And it can take negative values.) Continue reading

Posted in Development | Tagged , | Comments Off

Rails 2.1: Aggregate Expressions

Rails 2.1 allows you to perform complex aggregate queries, such as Donation.sum(“amount * 3″). This makes complex calculations a snap! Rails provides functions for calculating the average, count, maximum, minimum, and the sum. Continue reading

Posted in Development | Tagged , , | Comments Off

Rails 2.1: Named Scope

Rails 2.1 introduces something called named scopes; named scopes allow you to create named collections on a class-level for your models, such as Article.published. You can chain named scopes together, and even create parameterizable scopes by using the lambda keyword; you can pass in variables, of a sort, and Rails will plug the values into your named scope queries. Continue reading

Posted in Development | Tagged , , | 4 Comments

Rails 2.1: The First and the Last

Rails always made it easy to grab the first item in any ActiveRecord model. Rails 2.1 allows you to easily grab the last element, too, with the syntax ActiveRecord.find(:last). Like :first, you can add limits, conditions, etc. to it. Additionally, there are new static methods to allow you to call ActiveRecord.first and ActiveRecord.last to get the first and last elements respectively. Yay! Continue reading

Posted in Development | Tagged , , | Comments Off

Find_by_SQL

How can you use the find function if you have complex find criteria–such as searching by fields that don’t exist in the table for your class? Enter the find_by_sql function. It allows you to use arbitrary SQL to find the model records you want–everything from joins and where clauses to having clauses. Rails handles the nitty-gritty work of returning models that have the attributes you selected. Continue reading

Posted in Development | Tagged , , , | Comments Off

Undefined Method Foo

Sometimes, you add an attribute to a class, but when you try and access it, it tells you that the method is undefined. What’s going on? In this article, we discuss a bit about how Rails searches for things (database and code). The answer is probably that you forgot the getters and setters for that attribute! Continue reading

Posted in Development | Tagged , , , , | Comments Off

Validation 101

Data validation is crucial to web applications–especially e-commerce applications, where money is involved. Bad data can destroy the usefulness of (or even shut down) an application! What’s more, validation needs to be centralized–you want to keep your validation in one … Continue reading

Posted in Development | Tagged , , , , | 1 Comment

When New Fails

Sometimes, you say @foo = Foo.new(…) and then @foo.save. But it doesn’t save! In fact, creating the new instance failed! What’s going on? One possibility is that @foo is nil (so it wasn’t created properly). Another possibility is that @foo.id is nil–which means @foo failed validation! Continue reading

Posted in Development | Tagged , , , , | Comments Off

Efficiently Incrementing Model Attribute Values

Imagine you have a collection of links, and you’re tracking views–you want to easily increment the number of views every time someone clicks on the link. How can you do this? We discuss a number of approaches, from saving the model instance to updating the attribute to using the increment and decrement functions. Continue reading

Posted in Development | Tagged , , | 2 Comments

Using SQL for complex SELECT/ORDER Criteria

How can you select model objects using a complex SQL criterion (such as ordering by a SUM of values)? The answer is to use the find_by_sql method. You can select complex fields, join tables, and sort on those fields–such as sum fields. Continue reading

Posted in Deployment | Tagged , , | Comments Off