activerecord

Why all the Active Record hate?

As I learn more and more about OOP, and start to implement various design patterns, I keep coming back to cases where people are hating on Active Record. Often, people say that it doesn't scale well (citing Twitter as their prime example) -- but nobody actually explains why it doesn't scale well; and / or how to achieve the pros of AR w...

Any tips on getting Rails to run with an Access back-end?

I shudder to ask, but my client might offer no other SQL (or SQL-like) solution. I know Access has some SQL hooks; are they enough for basic ActiveRecord? Later: I appreciate all the suggestions to use other databases, but trust me: I've tried convincing them. There is an "approved" list, and no SQL databases are on it. Getting some...

How would you test observers with rSpec in a Ruby on Rails application?

Suppose you have an ActiveRecord::Observer in one of your Ruby on Rails applications - how do you test this observer with rSpec? ...

How can I count the number of records that have a unique value in a particular field in ROR?

I have a record set that includes a date field, and want to determine how many unique dates are represented in the record set. Something like: Record.find(:all).date.unique.count but of course, that doesn't seem to work. ...

Do you know how to implement transactions in Castle ActiveRecord?

I decided to make a system for a client using Castle ActiveRecord, everything went well until I found that the transactions do not work, for instance; TransactionScope t = new TransactionScope(); try { member.Save(); //This is just to see transaction wo...

Database sharding and Rails

What's the best way to deal with a sharded database in Rails? Should the sharding be handled at the application layer, the active record layer, the database driver layer, a proxy layer, or something else altogether? What are the pros and cons of each? ...

How can I upsert a bunch of ActiveRecord objects and relationships in Rails?

I am working with an API that provides bus arrival data. For every request, I get back (among other things) a list of which routes serve the stop in question. For example, if the list includes result for bus route #1, 2, and 5, then I know that those serve this stop. I have a many-to-many relationship set up between Route and Stop, and ...

activerecord as model, is this a good idea?

Recently thanks to rails' popularity, many people start using activerecord as model. however, before I heard of rails (my peer group was not a fan of open source stuff, we were taught in a .NET school...) and while I was doing my final year project, i found this definition for a model The model represents enterprise data and the busi...

How to create multiple records at once with ActiveScaffold in ROR

I am wanting to use ActiveScaffold to create assignment records for several students in a single step. The records will all contain identical data, with the exception of the student_id. I was able to override the default form and replace the dropdown box for selecting the student name with a multi-select box - which is what I want. Th...

What is the easiest way to duplicate an activerecord record?

I want to make a copy of an activerecord record, changing a single field in the process (in addition to the id). What is the simplest way to accomplish this? I realize I could create a new record, and then iterate over each of the fields copying the data field-by-field - but I figured there must be an easier way to do this... such as:...

Best Practice for Model Design in Ruby on Rails

The RoR tutorials posit one model per table for the ORM to work. My DB schema has some 70 tables divided conceptually into 5 groups of functionality (eg, any given table lives in one and only one functional group, and relations between tables of different groups are minimised.) So: should I design a model per conceptual group, or should...

rake db:migrate doesn't detect new migration ?

Experienced with Rails / ActiveRecord 2.1.1 You create a first version with (for example) ruby script\generate scaffold product title:string description:text image_url:string This create (for example) a migration file called 20080910122415_create_products.rb You apply the migration with rake db:migrate Now, you add a field to the produ...

Overriding "find" in ActiveRecord the DRY way

I have a few models that need to have custom find conditions placed on them. For example, if I have a Contact model, every time Contact.find is called, I want to restrict the contacts returned that only belong to the Account in use. I found this via Google (which I've customized a little): def self.find(*args) with_scope(:find => { ...

in Rails, how do I execute direct SQL code on a different database?

Here is the case. I'm writing a Rails application which will monitor data quality over some specific databases. In order to do that, I need to be able to execute direct SQL queries over these databases - which of course are not the same as the one used to drive the Rails application models. In short, this means I can't use the trick of ...

Mixing ActiveRecord find Conditions

I want to find records on a combination of created_on >= some date AND name IN some list of names. For ">=" I'd have to use sql condition. For "IN" I'd have to use a hash of conditions where the key is :name and the value is the array of names. Is there a way to combine the two? ...

In Rails, after using find with :select, my objects don't save

Running something like: @users = User.find(:first, :select => "name, lastname, salary") for @user in @users do @user.salary = 100000 @user.save end After this looking up in the Mysql table, the users aren't updated. ...

How does has_one :through work?

I have three models: class ReleaseItem < ActiveRecord::Base has_many :pack_release_items has_one :pack, :through => :pack_release_items end class Pack < ActiveRecord::Base has_many :pack_release_items has_many :release_items, :through=>:pack_release_items end class PackReleaseItem < ActiveRecord::Base belongs_to :pack belo...

How does the ActiveRecord pattern differ from the Domain Object or Data Mapper pattern?

I was looking at DataMapper, which appeared at first glance to use the ActiveRecord ORM pattern. Other people said that it uses the DataMapper and/or the Domain Object pattern. What is the difference between those patterns? ...

Stop Activerecord from loading Blob column

How can I tell Activerecord to not load blob columns unless explicitly asked for? There are some pretty large blobs in my legacy DB that must be excluded for 'normal' Objects. ...

Should you avoid object inheritence when persisting to an RDBMS solution

While looking at this question: Why all the Active Record hate? I found myself thinking of a general theme that ActiveRecord pattern is good when you have little or no inheritence in your objects, and DataMapper, while more complex, works much better if you do. Given that its never possible to perfectly match objects to relational datab...