activerecord

What's the best way to implement gmail style "undo" in Rails?

I think it important to have an "undo" method ala gmail when destroying records instead of displaying an annoying popup that says, "Are you sure?". The way that I've implemented this is to have a "deleted_at" timestamp column in the model which gets timestamped when destroy method is called def destroy @foo = Foo.find(params[:id]) ...

How do I force ActiveRecord to reload a class?

I'm creating a bunch of migrations, some of which are standard "create table" or "modify table" migrations, and some of which modify data. I'm using my actual ActiveRecord models to modify the data, a la: Blog.all.each do |blog| update_some_blog_attributes_to_match_new_schema end The problem is that if I load the Blog class, then m...

Find records with more than one ActiveRecord HABTM Association

I've got two models, joined by a Has and Belongs To Many join table. Lets call these models User and Event. The majority of Users have 0 events, while few have one or more. I want to do something like: User.find(:all, :joins => :events, :conditions => ["'Something that count the events' > ?"], 0) The problem is, I'm not sure how to s...

Column order of results from rails ActiveRecord find_by_sql call.

I'm attempting to put together some basic report screens. I've got some fairly complicated SQL queries that I'm feeding into ActiveRecord's find_by_sql method. The problem I am having here is that I am losing the order of the columns as given in the original query. I'm assuming that this is because the Hash class does not preserve ent...

How to keep ActiveRecord association DRY?

Hi people I have an issue with trying to keep AR finders DRY in my application. I have created a blogging application which fetches all the related pages,posts,links,tags and categories for a blog when a user first views it. A sample show action for the Blog controller is shown below: def show #find blog by user name @user= Use...

Rails and CouchDB - Architectural Concerns

I am working on a project that is going to use CouchDB for flexible storage of documents. The requirements of my system are a neat match for CouchDB for storage. BUT My question really boils down to this: Should I keeop using ActiveRecord and MySQL as well ... there are a raft of handy Plugins that are all readily available for use wit...

Given an array of ActiveRecord objects, can I easily collect their relationships through a method call?

Let's say I have the following code: @sites = Site.find(session[:sites]) # will be an array of Site ids @languages = Language.for_sites(@sites) for_sites is a named_scope in the Language model that returns the languages associated with those sites, and languages are associated with sites using has_many through. The goal is for @langua...

How can I see the SQL ActiveRecord generates?

I'd like to check a few queries generated by ActiveRecord, but I don't need to actually run them. Is there a way to get at the query before it returns its result? ...

create object from xml string in ruby

I am trying to deserialize an object from xml in ruby. Something simple like u = User.new({:client_key => "Bar"}) v = User.new(u.to_xml) I get an error NoMethodError: undefined method `stringify_keys!' for #String:0x20fc7cc>. I'm not sure what I have to do in order to get the string from xml to an object. Update: @avdi gave me the...

NHibernate/ActiveRecord - Any way to map to a foreign key column only?

I'm using Castle ActiveRecord, but this question applies to NHibernate, too, since a solution that works with NHibernate should work for ActiveRecord. Anyway, what I have is an underlying table structure like this: TableA -hasMany-> TableB I have corresponding objects EntityA and EntityB. EntityA has an IList of EntityB objects. Thi...

ActiveRecord find starts with

Really simple question - how do I do a search to find all records where the name starts with a certain string in ActiveRecord. I've seen all sorts of bits all over the internet where verbatim LIKE SQL clauses are used - but from what I've heard that isn't the 'correct' way of doing it. Is there a 'proper' Rails way? ...

Activerecode HABTM primary key problem

I have to tables that have a many to many relationship. I have created the correct table codesecure_project_tst_definition and it works. I can join rows together by calling the codesecure_projects << method on a TstDefinition object. The problem is that for some reason active record wants to use Codesecure_project_id as the id value f...

How can I find a model's relationships?

I want to, when given a particular model, return all the related models it is associated with. For example: class Dog < ActiveRecord::Base has_many :bones belongs_to :master end d = Dog.first d.associations #<== should return [Bone, Master] Is there a way to do this already without having to roll my own? Failing that, any sugge...

Building dynamic fields using ActiveRecord::Serialization.to_xml

I'm curious about people's experiences using AR's to_xml() to build non-entity fields (as in, not an attribute of the model you are serializing, but perhaps, utilizing the attributes in the process) from a controller. to_xml seems to supply a few options for doing this. One is by passing in references to methods on the object being...

How to drop a validation set in included module?

Is there a way to drop a validation that was set in Rails plugin (or included module)? Let's say I have some model with module included in it: class User < ActiveRecord::Base include SomeModuleWithValidations # How to cancel validates_presence_of :something here? end module SomeModuleWithValidations def self.included(base) ba...

How to achieve versioned ActiveRecord associations?

I want to work with versioned ActiveRecord associations. E.g., I want to find the object that another object belongs_to as of a certain past date, or the one that it belonged to before that. Does there already exist a library subclassing Rails' ActiveRecord to provide versioned relations? Or some other Ruby library which provides persist...

Freezing associated objects

Does anyone know of any method in Rails by which an associated object may be frozen. The problem I am having is that I have an order model with many line items which in turn belong to a product or service. When the order is paid for, I need to freeze the details of the ordered items so that when the price is changed, the order's totals a...

Best practice for limiting the number of associations within a has_many relationship?

Say that I have two models- Users and Accounts. Each account can have at most n users associated with it, and a user can only be associated with one account. It would seem natural to say that User belongs_to :account and Account has_many :users However, I'm not clear on the best practice when it comes to limiting the number of ...

Does ActiveRecord handle locks and updates?

ActiveRecord doesn't seem to cater for record locking and database updates. How does one synchronise updates in disconnected multi-user environments such as websites and web services? ...

What's the cleanest way to override ActiveRecord's find for both models and collections?

I have library code that overrides Ar's find method. I also include the module for all Association classes so both MyModel.find and @parent.my_models.find work and apply the correct scope. I based my code off of will_paginate's: a = ActiveRecord::Associations returning([ a::AssociationCollection ]) { |classes| # detect http://dev.rub...