activerecord

What's the best way to store the ActiveRecord Models with Versions and their Associations with Versions?

If all I have is one model (for example Wiki) and want to save it along with its versions, I could use acts_as_versioned plugin which stores the wikis in "wikis" table and its versions in "wikis_versions" table. This is plain an simple even if I want to moderate the latest version before showing it to the public using a field as status w...

Get the SQL that would be executed from a certain method or named_scope

Given an ActiveRecord method or named_scope chain, is there a way to return the SQL that will get executed, without actually running it? e.g. Vote.positive.count.sql #=> "SELECT COUNT(*) FROM votes WHERE value > 0" Is there a built-in way to do this or a plug-in that offers this functionality? If not, any clues to where I can start t...

How do you iterate over active record objects in Ruby On Rails?

This question is quite simple but I have run into the problem a few times. Let's say you do something like: cars = Vehicle.find_by_num_wheels(4) cars.each do |c| puts "#{c.inspect}" end This works fine if cars is an array but fails if there is only one car in the database. Obviously I could do something like "if !cars.length.nil?...

Rails, using time_select on a non active record model

I am trying to use a time_select to input a time into a model that will then perform some calculations. the time_select helper prepares the params that is return so that it can be used in a multi-parameter assignment to an Active Record object. Something like the following Parameters: {"commit"=>"Calculate", "authenticity_token"=>"e...

Rails: How to implement counter caching with self-referential Many to Many via has_many :through

How can I roll my own counter cache for a self-referential many-to-many relationship that uses has_many :through? I need to track the number of citations and references for each article I'm using roughly the code from the answer to this question: class Publication < ActiveRecord::Base has_many :citations has_many :cited_publicatio...

How to sort Rails AR.find by number of objects in a has_many relationship

How can I write an AR find query to have the results ordered by the number of records in a has_many association? class User < ActiveRecord::Base has_many :photos end I want to do something like... User.find(:all, :order => photos.count) I realize my find is not valid code. Say I have the following data. User 1, which has 3 phot...

Multiple has_many_polymorphs in one model

I'm trying to define multiple polymorphic relations (has_many_polymorphs plugin) from a single parent to same children. Note has many viewers Note has many editors Viewers could be either Users or Groups Editors could be either Users or Groups Permission is the association model with note_id, viewer_id, viewer_type, editor_id, editor_ty...

Castle ActiveRecord - Always updating children, why?

Dear All: Hi! These last two weeks have been my first experience with Castle ActiveRecord, and with the ActiveRecord pattern in general. I'm working on a large system that uses it frequently, and I have been finding some strange SQL transaction issues (such as the one below) as I work on it. I'll give a simplified version of the one ...

Does LINQ to SQL use the ActiveRecord Pattern?

I've just been researching the ActiveRecord pattern, and based on this (http://en.wikipedia.org/wiki/Active_record_pattern), it seems Linq 2 Sql more or less implements this, am I wrong? or what would need to be changed for it to conform to the ActiveRecord pattern? ...

ActiveRecord Models - adding custom methods

Hi, I have 10 models and 3 of them need some additional custom methods which happen to be: has_staged_version? apply_staged_version discard_staged_version I want to define these methods once. I could create a custom subclass of ActiveRecord:Base, define the methods there, and have my 3 models inherit from it. But is there a more "Ru...

Changing the active_resource_store database for sessions in rails 2.3.2

There's a great answer for my question over here http://stackoverflow.com/questions/378376/rails-shared-sessions-with-activerecord but it has to do with rails 2.2.2. The method used has been deprecated in 2.3.2. Does anyone know how to use an external database for session data in rails 2.3.2? The overall goal is session sharing betwee...

How do you talk to a BerkeleyDB database from Ruby or Ruby on Rails?

I have no idea how I would set up a BerkelyDB database in a Ruby or Rails project. Does anyone have any experience configuring one, that they could talk about? Maybe using ActiveRecord or Datamapper? ...

How to iterate ActiveRecord Attributes, including attr_accessor methods

I've looked everywhere for an elegant solution. The essential problem seems to be that ActiveRecord attributes that map to database columns are handled completely differently in ActiveRecord::Base than attr_accessor methods. I would like to do something like: model.attribute_names.each do |name| # do stuff end in a way that also i...

How to set has_many_polymorphs CRUD parameters restfully

Working on a simple example of double sided polymorphic relationships using the has_ many_ polymorphs ActiveRecord plugin. I have two classes, "cats" and "dogs", which can have "friendships" with each other, so three classes in all: "cats", "dogs" and "friendships". A cat can be friends with a dog (yes, it does happen!) and of course als...

Initializing a Module mixed in to a Model

Hi, I have this: class Bullet < ActiveRecord::Base include StagedVersionMethods ... end And this module StagedVersionMethods def initialize puts self.bullet_id end end When I create an instance of Bullet, the modules initialize method fires, but I get an ActiveRecord error: ...activerecord-2.2.2/lib/active_recor...

What are some good or bad design decisions in ActiveRecord's implementation?

I'm interested in learning more about how ActiveRecord was designed and why particular decisions were made along the way that led to the implementation that we have now. Could anyone provide some examples of good or bad design decisions that were made in ActiveRecord's implementation? ...

Rails ActiveRecord::find Join statement issue

Why does the following rails statement User.find(:all, :joins => [:roles,:roles_users], :conditions => { :roles => { :name => 'subscriber', :authorizable_id => self.id, :authorizable_type => self.class.to_s }}) Translates into this (with 2x the ...

ActiveRecord conditions of an association (Rails)

Pretend I have a model, Post which has_many :comments. How can I only display posts that have comments? I am somewhat comfortable with named_scope but I don't know how I can put Post.comments (or self.comments) in the :conditions hash which expects symbols. class Post < ActiveRecord::Base has_many :comments named_scope :with...

Array to Active Record Model

Hi everyone! I want to show in a view the tasks for a day in concrete (today). these tasks have an estimated time that must sum 6 hours per day. in the model I have this method: def self.tasks_for_today total_time = 0 r = Array.new all(:conditions => "finished = 0").each do | t | total_time += t.estimated_time r << t if ...

My Rails queries are starting to get complicated, should I switch to raw SQL queries? What do you do?

My Rails app is starting to need complicated queries. Should I just start using raw SQL queries? What is the trend in the Rails community? Update: I do not have written queries right now, I wanted to ask this question before I start. But here is an example of what I want to do: I have books which have categories. I want to say- ...