Search
Categories
- Deployment (4)
- Development (63)
- Gems and Plugins (4)
- News (2)
- Projects (4)
- Testing (3)
Most Popular Tags
activerecord architecture collections css database date and time debugging design design patterns DRY ebooks fixtures floating-point numbers formatting framework front page global variables helpers I18n introductory launchpad meta-information migrations model multiplicity mvc MySQL partials production rails 2.0 rails 2.1 Rails 2.2 regular expressions routing rss ruby security setup sql UI upgrading user management validation xml xml builder
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
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
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
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
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
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 activerecord, attributes, debugging, framework, introductory
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
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 activerecord, debugging, introductory, new keyword, validation
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
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