activerecord

How do I define synthetic attributes for an ActiveRecord model?

I have an ActiveRecord model whose fields mostly come from the database. There are additional attributes, which come from a nested serialised blob of stuff. This has been done so that I can use these attributes from forms without having to jump through hoops (or so I thought in the beginning, anyway) while allowing forwards and backwar...

how do I write that join query with ActiveRecord?

anyone know how I would write that query with AR? select *, (m.user_id=1) as member from band b join memberships m on m.band_id = g.id; Thanks in advance. ...

SearchLogic + STI

Trying to implement a search logic search that uses associations with STI but I’m having a problem where it is not select the STI records as the subclass but the parent. Example: class Users end class Artist < User has many :agents, :through => :agents artists end class Agent < User has many :artists, :through => :agents artists...

has_many :through, nested polymorphic relations

Is there a way to directly reference (using rails directly, without resorting to a lot of custom SQL) a relation that is nested behind a polymorphic relation? In the below example, is there a way to define a has_many relation in User that references LayerTwo? I'd like to do (in User) has_many :layer_twos, :through => layer_ones but...

ActiveRecorded associated model back reference

Hello, It is easy to associate a model to another using has_many/belongs_to methods. Let's suppose the following models: class Movie < ActiveRecord::Base has_many :actors end So, I can find the actors from a given movie instance. But now, given an actor instance obtained through the actors association, I'd like to find the movie ...

dependent => destroy on a "has_many through" association

Apparently dependent => destroy is ignored when also using the :through option. So I have this... class Comment < ActiveRecord::Base has_many :comment_users, :dependent => :destroy has_many :users, :through => :comment_users ... end ...but deleting a Comment does not result in the associated comment_user records getting deleted...

Raising an ActiveRecord error on RecordNotFound

Is there a way of doing a find_by_x that raises an exception if the record is not found? ...

What is easiest way to retreive one column's values using SubSonic (Column isn't primary)

I'm using AcriveRecord in SubSonic 3.0.0.3 with MySql. Is there some easy way to perform something like "SELECT CustomerAddress FROM customers" rather than .All() which performs "SELECT * FROM customers"? And how to get distinct values? ...

How can I make a cascade deletion in a one_to_many relatonship in Rails ActiveRecord?

I have a model in rails with one_to_many relationship. When I delete the father, I'd like to delete all childrens. How should I do it? I want to delete all orders and its items when I delete a user My models are: class User < ActiveRecord::Base has_many :orders, :foreign_key => "id_user" end class Order < ActiveRecord::Base has_ma...

What is the purpose of Active Records?

I'm tinkering with CodeIgniter and have come across Active Records for the first time. At first I dismissed it as something for people who don't really know how to write SQL. I realise now that my analysis was flawed and Active Records are pretty prominent, especially in Rails. But what purpose do Active Records hold? Is it to abstra...

Problem creating ActiveRecord model: data missing from save

Hi all, I'm having trouble creating a new model row in the database using ActiveRecord in a Sinatra app I'm developing. The object in question is being created without any errors (using save!, no exceptions are raised), but most of the data I specify for the save is not present. class ProjectMeta < ActiveRecord::Base attr_accessor ...

SubSonic 3 ActiveRecord migration ignores foreign keys

Using SubSonic 3 ActiveRecord, I generated code from an existing database that had foreign keys. To ensure database schema is always correct when switching databases, I placed migration code at the beginning of the app, using IDataProvider.MigrateToDatabase<MyClass>() for each class generated by ActiveRecord.tt. Turns out, migration code...

Find all rows ordered by method in ActiveRecord

I have an ActiveRecord model with a field called name (in the database) and a method called collation_name (not in the database) that returns a reduced version of the name. The model class resembles this: class Foo < ActiveRecord::Base # ... other stuff ... def collation_name words = name.downcase.split if words[0] == 'the...

Validate uniqueness of many to many association in Rails

Say I have Project, that is in many-to-many association with Tag. I'm using *has_many through* so I have separate join model. How do I create validation, that checks uniqueness of join model? Now I have only has_many :tags, :through => :taggings, :uniq => true But that doesn't validate on save. ...

Is there a better way to get this data?

Photographers "have_many" clients. Clients "have_many" events. Is there a better way to assign @events here if the user is a photographer? def index if @current_user.photographer? @events = [] @current_user.clients.each do |client| @events << client.events end else @events = @current_user.even...

ActiveRecord find with association details

Suppose I am doing matchmaking of users and games. I have models containing users and games. class Game < ActiveRecord::Base has_and_belongs_to_many :users class User < ActiveRecord::Base has_and_belongs_to_many :games Games can have many users, users can be playing many games. Because of HASBM I have a table called games_use...

Recursive :include in Rails ActiveRecord

Say I have these models class Project < ActiveRecord::Base has_many :comments end class Comment < ActiveRecord::Base belongs_to :project belongs_to :user end class User < ActiveRecord::Base has_many :comments end So that I can do p = Project.find(1, :include => :comments) p.comments.collect(&:user).collect(&:name) #...

Optimizing ActiveRecord Point-in-Polygon Search

Hello stackies. The following PiP search was built for a project that lets users find their NYC governmental districts by address or lat/lng (http://staging.placeanddisplaced.org). It works, but its kinda slow, especially when searching through districts that have complex polygons. Can anyone give me some pointers on optimizing this code...

IronRuby ActiveRecord and SQLite

I see that the IronRuby team has documented using ActiveRecord in IronRuby with MSSQL - they indicate that some adjustments were required to the adapter. In interview on 8/11/2009, Jimmy indicates a major drawback of IronRuby: The main limitation is that IronRuby does not support any of the C-based Ruby libraries That indica...

Naming conventions for Rails migrations

Is there a best practice naming convention for Rails migrations, particularly when editing a model? e.g. if I'm adding a column bar to the Foo model, should I name it edit_foo or add_bar_to_foo I'm assuming if I'm editing mutliple models then I should create multiple migrations, but what if I'm making multiple modifications to a single...