activerecord

Can Rails' Active Record handle SQL aggregate queries?

Hi, Just started learning active record and am wondering how to best retrieve data from multiple tables where an SQL aggregate query is involved. In the following example (from a medical app) I'm looking for the most recent events of various types for each patient (e.g. last visit, last labtest etc). As you can see from the sql query be...

Talk to data warehouse-style tables with ActiveRecord?

As my Rails app matures, it's becoming increasingly apparent that it has a strong data warehouse flavour, lacking only a facts table to make everything explicit. On top of that, I just read Chapters 2 (Designing Beautiful APIs) and 3 (Mastering the Dynamic Toolkit) of Ruby Best Practices. Now I'm trying to figure out how best to design...

CodeIgniter Active Record - Get number of returned rows

I'm very new to CodeIgniter and Active Record in particular, I know how to do this well in normal SQL but I'm trying to learn. How can I select some data from one of my tables, and then count how many rows are returned using CodeIgniters Active Record class? Thanks, Tom. ...

Cache all models in a table

I need to cache (and expire) all the models in a table. For example, if i have a model named Currency, i only have less than 10 possible currencies. Therefore, it would be nice to have: class Currency < ActiveRecord::Base cache_all(:expire_in => 10.minutes) end so that Currency.all Currency.find_by_name("USD") should not hit the...

Catch-22: Rails db migration doesn't run when class constants make db calls

I have a class similar to the following: class FruitKinds < ActiveRecord::Base Apple = FruitKinds.find(:all).find { |fk| fk.fruit_name == :apple.to_s } # ... other fruits # no methods end Apple and other specific Fruits are commonly used as default values elsewhere in my application, so I want a handy means to refer to t...

How do i work with two different databases in rails with active records?

I need 2 db connections in my rails model, is there a not so hackey way to do that? any links or search keywords would be great :) ...

Is there an ActiveRecord way to do this SQL query?

Have Addresses and Lists with many-to-many relationship, as shown below. Sometimes need all the Lists an Address is not in. Using the find_by_sql query shown, and it works great. But is there a way to do it without using direct SQL? class List has_many :address_list_memberships has_many :addresses, :through => :address_list_member...

Rails: Finding children of children in habtm activerecord relationships

I have 3 classes related by habtm associations...Users, Locations, Employees. Users is related to Locations via a habtm relationship, and Locations is related to Employees via a habtm relationship. What I would like to be able to do is say: current_user.locations.employees Does anyone know the "Rails Way" to do this? I can do it in ...

PHP data access design patterns to complement an ORM

I've currently got a site that depends on an Active Record pattern using the Doctrine ORM in PHP. I'm generally a fan of this approach - it's very straightforward and works well for managing for simple CRUD apps. But as this site grows, I think my need for more robust domain functionality will grow as well. I was wondering what other kin...

ActiveRecord primary key is a varchar, error when saving

I have an active record class class Service_Catalogue < ActiveRecord::Base set_table_name "service_catalogue" set_primary_key "myKey" end myKey is an nvarchar (sql server). When I try and save it service_catalogue= Service_Catalogue.new() service_catalogue.myKey = "somevalue" service_catalogue.save I get the fol...

get a list of all models from rails

Hi I need a list with all models (class_names) which have the pattern "Cube" at the end. example: all my models: ModelFoo, ModelBar, ModelBarCube, Mode2BarCube what I need: ['ModelBarCube', 'Mode2BarCube'] ...

What does has_many_polymorphs mean by "Referential integrity violation"?

I have a has_many_polymorphs relationship between a Candidate and many events of various type. In particular, a Candidate creates a Created event when it is created. class Candidate < ActiveRecord::Base has_many_polymorphs :events, :through => :candidate_events, :from => Event::Base.included_in_classes.m...

Sequel in conjunction with ActiveRecord any gotchas?

I'm considering using Sequel for some of my hairier SQL that I find too hard to craft in Active Record. Are there any things I need to be aware of when using Sequel and ActiveRecord on the same project? (Besides the obvious ones like no AR validations in sequel etc...) ...

RoR ActiveRecord dynamic finders: lying about interface?

Do Ruby on Rails' ActiveRecord dynamic finders have a problem in that they don't explicitly tell clients about their interface? Clients = client code or developers creating the client code. What uses the ActiveRecord. If you use the active record, is it clear from looking at the class what you can do with it, what it needs or how you ca...

Is there a name to this Ruby on Rails common model pattern? Polylink?

There seems to be no name for this common model pattern. It's used in many plugins like acts_as_taggable[_whatever] and it basically allows linking a certain model, like Tag, with any other model without having to put ever-more belongs_to statements in the Tag model. It works by having your model (Tag) linked to a polymorphic join mo...

rails group by multiple columns

i have budgets table with emptype_id and calendar_id actual_head, estimated_head when i do Budgets.sum(:actual_head ,:group=>"emptype_id,calendar_id") i do not get the result grouped by the above two columns but only by the emptype_id however when i check the log the sql query is right "SELECT sum(budgets.actual_head) AS sum_ac...

Rails Migrations: Load default data

Best way to load seed data? I have an Author table that is tightly coupled with a Users table. I also have migrations to alter both of these tables. I want to add a default admin user but I want to make sure that both tables are created and all migrations have run for these tables before my CreateDefaultAdmin (or whatever) migration runs...

how to delay the creation of the association until the subclass is created in active record?

I have four tables: jp_properties, cn_properties, jp_dictionaries and cn_dictioanries. And every jp_property belongs to a jp_dictionary with foreign key "dictionary_id" in table. Similarly, every cn_property belongs to a cn_dictionary with foreign key "dictionary_id" in table too. Since there are a lot of same functions in both proper...

Changing a Active Record DB in Rails

I have a rails app I created with based on a DB with a table named "countries", which stored a list of all the countries in the world. Now I have found out that the actual DB I will be using uses a table named "ctry" instead (stupid I know). What I am trying to figure out is if there is any way I can point active record to this new table...

What is the difference between build and new on Rails?

Can anyone tell me what is the difference between build and new command on Rails? ...