activerecord

Grouping Activerecord query by a child attribute

Is it possible to use an attribute of a child to group a query? Post.find(:all, :include => [ :authors, :comments ], :group=>'authors.city') does not work. However, I am able to use author.city as part of the conditions. ...

How to eager load objects with a custom join in rails?

Background Normal rails eager-loading of collections works like this: Person.find(:all, :include=>:companies) This generates some sql which does LEFT OUTER JOIN companies ON people.company_id = companies.id Question However, I need a custom join (this could also arise if I was using find_by_sql) so I can't use the vanilla :includ...

How to best handle per-Model database connections with ActiveRecord?

I'd like the canonical way to do this. My Google searches have come up short. I have one ActiveRecord model that should map to a different database than the rest of the application. I would like to store the new configurations in the database.yml file as well. I understand that establish_connection should be called, but it's not clea...

Should I be extending this class? (PHP)

I'm creating an ORM in PHP, and I've got a class 'ORM' which basically creates an object corresponding to a database table (I'm aiming for similar to/same functionality as an ActiveRecord pattern.) ORM itself extends 'Database', which sets up the database connection. So, I can call: $c = new Customer(); $c->name = 'John Smith'; $c->sav...

Problem accessing previousState in OnFlushDirty() event of Castle ActiveRecord

I have this problem, I'm using Castle ActiveRecord and when I update I verify changes in the object in the OnFlushDirty event. However, when I access to the previouState["MyProperty"], it turns to be null and I can't get the old value. Do you know why? this is the code; protected override bool OnFlushDirty(object id, System.Colle...

From objects to tables using activerecord

Hi all, I'm getting some objects from an external library and I need to store those objects in a database. Is there a way to create the tables and relationships starting from the objects, or I have to dig into them and create migrations and models by hand? Thanks! Roberto ...

How to list of all the tables defined for the database when using active record?

How do I get a list of all the tables defined for the database when using active record? ...

Traversing HABTM relationships on ActiveRecord

I'm working on a project for my school on rails (don't worry this is not graded on code) and I'm looking for a clean way to traverse relationships in ActiveRecord. I have ActiveRecord classes called Users, Groups and Assignments. Users and Groups have a HABTM relationship as well as Groups and Assignments. Now what I need is a User func...

Why ActiveRecord instead of a MySql API

I've been developing web applications for a while and i am quite comfortable with mySql, in fact as many do i use some form of SQL almost every day. I like the syntax and a have zero problems writing queries or optimizing my tables. I have enjoyed this mysql api. The thing that has been bugging me is Ruby on Rails uses ActiveRecord an...

Why does ActiveRecord's serialize randomly corrupt my data?

I use serialize in one ActiveRecord model to serialize an Array of simple Hashes into a text database field. I even use the second parameter to coerce deserialization into Arrays. class Shop < ActiveRecord::Base serialize : recipients, Array end It seems to work fine but, after a few requests, the content of recipients turns to Hash...

Error Updating a record

I get a mysql error: #update (ActiveRecord::StatementInvalid) "Mysql::Error: #HY000Got error 139 from storage engine: When trying to update a text field on a record with a string of length 1429 characters, any ideas on how to track down the problem? Below is the stacktrace. from /var/www/releases/20081002155111/vendor/rails/activere...

Rails named_scopes with joins

I'm trying to create a named_scope that uses a join, but although the generated SQL looks right, the result are garbage. For example: class Clip < ActiveRecord::Base named_scope :visible, { :joins => "INNER JOIN series ON series.id = clips.owner_id INNER JOIN shows on shows.id = series.show_id", :conditions=>"shows.visi...

non-DB attr_accessor attribute persistence in Rails

I have an application in which attr_accessor is being used to keep temporary data for a model which will be passed to a rake task. Seeing there is not a database field for these attributes and they are not being calculated from database data, will the attr_accessor data persist and be available to the rake task? What happens if I need ...

ASP.NET MVC + ORM

I am in the start up of a project using ASP.NET MVC and have started creating my models. Since I know some Ruby On Rails and would like to use a system as similar to Rails Active Record as possible. Have anyone used Castle Projects Active Record in a ASP.NET MVC application (or any application that is relevant) and have some experience ...

How can I dynamically change the Active Record database for all models in Ruby on Rails?

In our program, each customer gets their own database. We e-mail them a link that connects them to their database. The link contains a GUID that lets the program know which database to connect to. How do I dynamically and programatically connect ActiveRecord to the right db? ...

Re-factoring from Active Record to Data Mapper

have you re-factored from an Active Record to a Data Mapper pattern? what conditions prompted the switch? i'm primarily interested in web based applications, but would like to know the challenges that accompany such a move in any environment. ...

How do I create an ActiveRecord relationship to an ActiveResource object?

Let's say I'm writing a Library application for a publishing company who already has a People application. So in my Library application I have class Person < ActiveResource::Base self.site = "http://api.people.mypublisher.com/" end and now I want to store Articles for each Person: class Article < ActiveRecord::Base belongs_to :...

Fetching Minimum/Maximum for each group in ActiveRecord

This is an age-old question where given a table with attributes 'type', 'variety' and 'price', that you fetch the record with the minimum price for each type there is. In SQL, we can do this by: select f.type, f.variety, f.price from ( select type, min(price) as minprice from table group by type ) as x inner join table as f on f....

How to extract common named_scopes from ActiveRecord models

I have named_scope which is reused in multiple ActiveRecord models. For example: named_scope :limit, lambda {|limit| {:limit => limit}} What is the best practice to extract this code to be shared across models. Is it possible to extract it to a module or should I rather reopen ActiveRecord::Base class? ...

Activerecord association question: getting has_many :through to work

I'm building an app in Ruby on Rails, and I'm including 3 of my models (and their migration scripts) to show what I'm trying to do, and what isn't working. Here's the rundown: I have users in my application that belong to teams, and each team can have multiple coaches. I want to be able to pull a list of the coaches that are applicable...