activerecord

How does class cacheing work in rails 2.2+

I have a rails application that patches ActiveRecord with a hand-coded validator. The patch is made by adding the following lines in config/environment.rb Rails::Initializer.run do |config| ... end class ActiveRecord::Base include MtLib::DBValidations end This works fine in production mode i.e. with config.cache_clas...

what would happen if you use belongs_to without having a corresponding has_one?

I have a core model for an Item, and a script to populate it from a custom source (think, RSS feed). In the feed, each Item is identified with a guid; in my system, Item only has an autogenerated id primary key. I want to have, let's say, ItemFeedInfo that maps guid->id (so that I can distinguish between new vs. modified Items) I'm thi...

Rails after_save callback to create an associated model based on column_changed?

Hi, I have an ActiveRecord model with a status column. When the model is saved with a status change I need to write to a history file the change of status and who was responsible for the change. I was thinking an after_save callback would work great, but I can't use the status_changed? dynamic method to determine that the history writ...

Rails Active Record Instance Variables

Hello My questions is in regards to this AR and its instance variable @saved class PhoneNumber < ActiveRecord::Base has_one :user validates_presence_of :number def self.create_phone_number( user, phone_hash ) @new_phone = PhoneNumber.new(phone_hash) @user = user PhoneNumber.transaction do @user.phone_numbers << @new_phone @new...

ActiveRecord running different queries in production?

I have a class hierarchy looks like this: class Post < ActiveRecord::Base; end class Project < Post; end class ProjectDesignWall < Project; end There's a controller that fetches data like so: @projects = Project.find(:all, :include => [:project_image_photos,:user]) In development, this runs the following query, straight from the lo...

Can you force activerecord associations to update without saving them?

I have a couple models which are associated with the has_many belongs_to pair. For the sake of demonstration, a client has one server but a server has many clients. I might do something like this: client1.server = the_server client2.server = the_server client3.server = the_server My actual application is quite a bit more complex tha...

How does versioning in ActiveRecord's migrator work?

Hi, I am trying to learn Rails by working with different packages (ActiveRecord, ActiveSupport) without rails gem. I can't figure out how to create a database with three classes, though. Here's my rakefile: require 'rubygems' require 'activerecord' require 'yaml' task :default => :migrate desc "Migrate the database through ...

Basic ActiveRecordStore updated_at field not being updated on every request

I'm using the standard active_record_store in my app. In environment.rb I have: config.action_controller.session_store = :active_record_store And my sessions table was created with rake db:sessions:create: create_table :sessions do |t| t.string :session_id, :null => false t.text :data t.timestamps end add_index :sessions, :se...

Polymorphic Association doesn't save ClassName (without STI), any hints??

Hi, I have two models, one called Notes and one called Comments. Comments can be associated to many other models so I use a polymorphic association. In the schema.rb it looks like this: create_table "comments", :force => true do |t| t.text "body" t.integer "user_id" t.integer "commentable_id" t.integer "commentable_type" t.dat...

Caching ActiveRecord object and associations

I want to be able to "deep clone" 10 instances of an ActiveRecord model and all its associations into memory, work on them, update the in-memory objects and then, when I've finished, pick one to write back over the original in the database. How do I deep clone (i.e. .clone but also cloning all associations right down to the bottom of t...

Connecting to SQL Server with ActiveRecord

Have you ever had to connect to SQL Server with ActiveRecord? Is this possible? Can anyone provide some starting points? ...

ActiveRecord conditions Error

Hi, I have this: SectionHistory.find(:all, :conditions => ["id=? and (name!=? or code!=?)", sec.id, sec.name, sec.code]) And I get the error "The character "!" following "and (name!=? or code" is not valid" if my condition string is just "id=? and (name!=?)" it's OK We're using DB2. Any suggestions on this? Thanks ...

Whats the best method of creating "walled gardens" for multiple clients inside your database?

I'm setting up a SaaS style website wherein I will have multiple clients all managing their workflows and data on the same site -- and thus the same database. I'm not even sure if there is a word for this concept, but is there any established way of auto-segregating the data so that any ActiveRecord calls to the database are filtered/re...

How can I determine if my ActiveRecord object violates a unique database key/index?

ActiveRecord's validates_uniqueness_of is vulnerable to race conditions. To really ensure uniqueness requires additional safeguards. One suggestion from the ActiveRecord RDocs is to create a unique index on the database, for example by including in your migrations: add_index :recipes, :name, :unique => true This will ensure at the d...

When creating an object in Ruby on Rails, which method of saving do you prefer, and why?

When writing the "create" method for an object in a Ruby on Rails app, I have used two methods. I would like to use one method for the sake of cleaner and more consistent code. I will list the two methods below. Does anyone know if one is better than the other? If so, why? Method 1: def create1 # is this unsecure? should we grab ...

Codeigniter: MySQL where clause, and quotes

Here's my query in Codeigniter: $this->db->select('comments.created_at, comments.section_id, comments.submittedby_id, users.username, comments.text, sections.name'); $this->db->order_by('comments.created_at', 'desc'); $this->db->where('comments.submittedby_id', 'users.user_id'); $this->db->where('comments.section_id', 'sections.id'); ...

Nested Object w/ Checkboxes - mass-assignment even with accepts_nested_attributes_for ?

I thought that there should have been a simple solution to this, given that Rails 2.3 has this newfangled nested forms feature. Basically I want to create or update a user and assign them roles at the same time. It seems like I'm doing everything right but I get the error WARNING: Can't mass-assign these protected attributes: roles_attr...

Rails : uninitialized constant error on Active Record destroy

Hello I am having an issue when trying to destroy an active record instance. It involves the following AR class Client < ActiveRecord::Base has_many :phone_numbers, :dependent => :destroy has_many :email_addresses, :dependent => :destroy has_many :user_clients , :dependent => :destroy has_many :users, :through => :user...

Renaming the created_at, updated_at columns of ActiveRecord/Rails

I want to rename the timestamp columns defined in timestamp.rb . Can the methods of timestamp.rb be overwritten? And what has to be done in the application that the module with the overwritten methods is used. ...

Sorting an array of ActiveRecord objects by a column

I need to sort an array of ActiveRecord objects by the value in one of the columns but am unsure of how to do this. Example, I have an Schedule model that has a Duration column. I then have two arrays of Schedule objects OriginalList and NewList. I want to sort each of those lists by Duration. I think that I'm looking for something like...