I've been considering switching from using PHP to Ruby on Rails for my web projects and the one thing I'm most concerned about is the use of the ActiveRecord for Rails. It seems to me like using the ActiveRecord is very strongly encouraged and writing straight SQL seems frowned upon. After looking at several projects I have yet to see one that actually uses SQL in it.
I'm not sold on the ActiveRecord yet, mostly because it requires learning something that doesn't seem as powerful as straight SQL, e.g. is there an easy way to do a nested query with a group by and having clause with the ActiveRecord or will I have to jump through hoops to do so?
Has anyone ever used Rails without the ActiveRecord? If so, what was your experience doing so?
One of the primary aspects of ActiveRecord is that there is very little to no configuration needed. It follow convention over configuration. ActiveRecord is commonly used with the Ruby-on-Rails framework but you can use it with Sinatra or without any web framework if desired.
For Rails 3 and Rails 4: Use -O (Capital 'O') or --skip-activerecord option to generate an application without a database. Notice the extra hyphen '-' as opposed to previous Rails versions.
ActiveRecord::Base indicates that the ActiveRecord class or module has a static inner class called Base that you're extending.
ActiveRecord is a gem that is part of Ruby on Rails. It is the ORM, i.e. the library that maps our objects to tables. In other words, it is the Ruby library that allows us to use Ruby classes in order to access our data stored in an RDBMS, like MySQL or PostgreSQL.
Three words: Here be dragons.
By going off the path of doing things "The Rails Way", then you will encounter foes of unimaginable power and cunning. At first, you will think "OH HO HO I am doing it my way, aren't I awesome?". Then you'll grow tired. Exhausted even. Forlornly, during your struggles, you'll look at all the cool gems that are using Active Record and wonder why you chose to venture down a path so fraught with doom and gloom it would make Chuck Norris shit his pants.
Please, do not stray from Active Record. It is only here to help you, not hinder you. If you wish to write your own queries then there is a find_by_sql
method, or an even lower Model.connection.execute
method. However, these should only be used, much like nuclear weaponry, in times of exceptionally dire needs.
If you don't want to use Active Record at all, then I would encourage you to look at either DataMapper or Mongoid.
DataMapper provides much of the same functionality as Active Record and some have been known to prefer the syntax. As a bit of trivia: it was one of the first gems to be compatible with Rails 3.
Mongoid on the other hand is for MongoDB databases, which some other people also fancy.
I beg again: do not stray from the beaten path, lest you want a beating yourself.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With