activerecord

Rails :dependent and :delete

Hello I am running Rails 2.0.2 and am unable to use :dependent => :delete in my AR associations has_many :items, :dependent => :delete I am given this error. The :dependent option expects either :destroy, :delete_all, or :nullify (:delete) I have be unable to find the documentation for :delete_all to see if it does what I want....

Is there a better way of filling a generic List from a SQL DataAdapter?

I have an existing application that uses Active Record for its data retrieval. It's in VB.NET (first time I'm doing VB.NET; I usually work in C#). And I'm building a method to return a List(Of T) of an object. The current pattern uses a SQLDataAdapter to populate a datatable. I COULD add the record to the List(Of T) as I fill the datat...

Rails Active Record find(:all, :order => ) issue.

I seem to be unable to use :order_by for more than one column at a time. For example, I have a "Show" model with date and attending columns. If I run the following code: @shows = Show.find(:all, :order => "date") I get the following results: [#<Show id: 7, date: "2009-04-18", attending: 2>, #<Show id: 1, date: "2009-04-18", atte...

does cloning affect activerecord callbacks?

I have a sequence of ActiveRecord objects that I want to cascade destroy but some of the objects are not being removed. Essentially I have as follows:- class Project < ActiveRecord::Base has_many :tasks, :dependent => :destroy has_many :schedules, :dependent => :destroy has_many :project_schedules, :through => :schedules, :class...

Rails (or maybe SQL): Finding and deleting duplicate AR objects.

ActiveRecord objects of the class 'Location' (representing the db-table Locations) have the attributes 'url', 'lat' (latitude) and 'lng' (longitude). Lat-lng-combinations on this model should be unique. The problem is, that there are a lot of Location-objects in the database having duplicate lat-lng-combinations. I need help in doing ...

Testing models with multiple database connections in Rails using ActiveRecord

What is the best way to test a model that is using a different database connection in Rails. For example I have a model FooBar that is read only: class FooBar < ActiveRecord::Base establish_connection configurations['foo_bars'] # ... end Are there any good conventions, hacks, or plugins out there? ...

How do you change your Rails App Data?

I have seen a lot of talk regarding ActiveRecord Migrations and whether or not they should be used to change data within your application, some people saying yes some saying no. My question is if you are not using Migrations to do this then what are you using? Just another script that you write? I am after suggestitions on alternative w...

Altering the primary key in Rails to be a string

So I've got two models, State and Acquisition. State has_many Acquisitions. I felt like an autoincrementing integer primary key for 51 records was rather silly. So I altered the model for the State to be the PK (State being the two letter abbreviation; I'm not storing the actual state name anywhere: class State < ActiveRecord::Base ...

How do you set an attribute when creating an ActiveRecord object?

I am trying to set an attribute on an object that I am creating. I feel like this should work: def create @album = Album.new(params[:album]) @album.user = current_user if @album.save flash[:notice] = 'Album was successfully created for ' + current_user.login + '.' redirect_to albums_url else rend...

Can ActiveRecord connect to PostgreSQL remotely and protect the DB password?

I have a PostgreSQL DB on a remote VPS server (CentOS 5) and I'd like to connect to have a Rails application connect to it from my local Mac laptop. On my laptop, I have the ActiveRecord PostgreSQL adapter installed -- postgres (0.7.9.2008.01.28). I read in the PostgreSQL docs: The password-based authentication methods are md5, cryp...

Ruby, windows, active_record, and Control-C

What is active_record doing to the signal processes under windows (I don't see this with the same versions on the mac) that causes it to behave so strangely? For instance: require 'rubygems' trap("INT"){puts "interrupted"} puts __LINE__ sleep 5 require 'active_record' trap("INT"){puts "interrupted again"} puts __LINE__ sleep 5 When I...

ActiveRecord fails to update HABTM relation

I'm using a simple model for user authorisation with two ActiveRecords User and Role User and Role have a HABTM relation to each other. I tried to created a user interface for assigning roles to users with simple checkboxes - just like in Railscasts Episode #17. My problem is that neither User#new nor User#update_attributes use the par...

Best practices for multiple associations with the same class in Rails?

I think my question is best described as an example. Let's say I have a simple model called "Thing" and it has a few attributes that are simple data types. Something like... Thing - foo:string - goo:string - bar:int That isn't hard. The db table will contain three columns with those three attributes and I can access them w...

Updating eager-loaded associations in memory

In my current rails app I'm pulling a lot of data from the database into memory so that I can start a long-running background task that I don't want to hit the database every 5 seconds. This is all working great but I have some inelegant code that I'd like to get rid of if possible. For an example, I have the situation where I have a U...

Conditional validation in ActiveRecord

I need to do conditional validation of models in Rails dependant on one of the fields within the model. This is a follow-up to an earlier question which prompted development of a solution that doesn't feel right. I have a number of ActiveRecord models all based around an 'Order Model', a shortened version of which is shown below. clas...

Why does active_record break the behavior of Ruby's trap and how do I work around it?

In the last couple of days, I've been trying to get a solution to an active_record issue that has been plaguing me. Posts on railsforum and stackoverflow have turned up completely dry. The length and level of detail in those posts may have dissuaded commentors, so I'm trying again - in brief. Under windows, the trap behavior is normal...

ActiveRecord does not work on App Engine - What's the alternative?

Early reports of JRuby on Google App Engine indicate that ActiveRecord does not work. It was my understanding that this was the only way to talk to the database in Rails. Is this not the case? And, if not, what is the alternative? Is there a more direct way in Rails of interfacing with Google's BigTable datastore? ...

Ruby on Rails: How to join two tables

I have an index page that I want to show all the users' profile and their associated photos. I'm using the plugin Paperclip for the photos. In the Profiles controller, I have the instance variable @profile but it shows me the table in the profiles table only and not the photos table. @profile = Profile.find(:all, :include => :photos, ...

Destroy associations after the last has_many :through record is deleted

With a regular has_many, there's the option of :dependent => :destroy to delete the associations when the parent record is deleted. With has_many :through, there might be other parents associated to the child records, so :dependent => :destroy doesn't have any effect. How do you ensure child records are deleted after they are orphaned f...

How do you set the value of a newly added ActiveRecord counter cache?

I have a model object which did not have a counter cache on it before and I added it via a migration. The thing is, I tried and failed to set the starting value of the counter cache based on the number of child objects I already had in the migration. Any attempt to update the cache value did not get written to the database. I even tried ...