Notice that, inside the block, we have to call first method to retrieve the object. ActiveRecord tries hard to defer the actual database query as long as possible, but in this case we need to run the query to get the data from the server. If we didn't try to access the data, the object we'd be trying to save to the cache would be an instance of ActiveRecord::Relation . Luckily, objects of this type can't be marshaled, and the save to the cache will fail with an exception. …
Simple CRUD app with ActiveRecord, SQLite3 and YAML
Using Rack Middleware
You can read through the RubyLearning FAQ .
Some Fun Apps
Here are some of the fun apps created by the previous batch participants and deployed to Heroku:
Stock Exchange Quote Service .
Using Sinatra to access the Google+ API .
A Sorter Web Service in Sinatra
Finding Photos …
write a shell script or something to strip out ActiveRecord and swap
in DataMapper, so if you're doing this, and you do a lot of
consulting, you might start to accrue a little library of post-
installation scripts.
If this continues for a long time, and your company develops a
sufficiently sophisticated set of consistent deviations from Rails
canon, it's almost like you have an alternate version of Rails.
In my opinion the alternative "stack" is really …
…fragment if the object hasn't changed, and re-create the fragment if the object is different. For ActiveRecord objects, Rails builds this key automatically!
This technique requires a cache store like memcache that can automatically get rid of older items that are no-longer used, otherwise your cache will grow uncontrollably.
Here's an example from our blog software - the cache key will be something like "blogposts/{id}-{updated_at}"
If you use ActiveRecord and have a bunch of settings in your database.yml file, you are making use of dependency injection. You are influencing a composition through arguments passed into the connection manager. You probably use this all over your own code, too—whenever you defer the creation of an object and allow that object to be overridden by something passed in via arguments. These are all cases of DI.
In other words: DI is not magical. Don't hate it.
But...
If you're …
ActiveRecord protects you against SQL injection by escaping input for you. For example the following works as expected, with no vulnerability: User.find_by_name("kotori'; DROP TABLE USERS; --") # => SELECT * FROM users WHERE name = 'kotori\'; DROP TABLE USERS; --' LIMIT 1
But ActiveRecord also defines ways for the programmer to inject SQL fragments into the query so that the programmer can customize the query when necessary. The injection interfaces are …
A SQL injection vulnerability has been found in ActiveRecord that impacts all versions of Rails:
I have released Hobo 1.3.3 that patches Hobo's vulnerability to this issue.
if you are using Hobo 2.0 it is recommended that you upgrade to Rails 3.2.10, although I have also pushed the security patch to github master.
The Hobo fix only impacts Hobo's usage. If you use find_by_ in your own code, you must fix those up yourself by coercing the …
…in some thread-safe data structures like queues and condition variables. Requiring ActiveRecord brings in a world of dependencies and causes a number of behavioral changes to Ruby that some consider impolite.
In some tinkerings with Clojure this weekend, I was struck how the ns function is more effective at both declaring dependency and coupling and in restricting the possible distress those qualities may bring. Consider this snippet from my weekend project: (ns hrq.routes (:use compojure.core …
Oh, and the fraud checking is no longer in a subclass of ActiveRecord::Base , so it's easier to test without Rails if that's what you're into.
Using a module
You could also use a module for this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 class Receipt < ActiveRecord :: Base include FraudCheckable end module Receipt::FraudCheckable def fraudulent? check_this check_that end private def check_this # ... end def check_that # ... end end
This has many of the same …
…Netzke lets you do much more than just quickly create a grid that seamlessly works with an ActiveRecord model without using any code generators. In fact, Netzke is best suited for making complex one-page web applications that make use of hundreds of data models and implement sophisticated workflows. One of the many techniques that Netzke as a modular GUI framework provides to you, is component nesting. Here I'll show you how easy it is to nest 3 differently configured instances …