activerecord

Rails: scaling with group calculations

Hello, currently I am running something similar to this command: Person.sum(:cholesterol, :group => :age) which seems to work great when I have a small number of records. However when I attempt to call this with a few thousand records or more in the table, it takes literally minutes to run. However, the SQL querey: SELECT sum(`peopl...

How to use datetime in the condtions of a find/count in Ruby on Rails

I'm trying to count the number of rows in a certain table by datetime. More specifically, by a certain month, but can't find the right way to write the conditions for it. xxx.count(:all, :conditions=> :xxx => yyy) I have a datetime yyy to compare with xxx, but only want to compare the year and month. ...

rails active record nuances and protecting against injection attacks

When I make a query... is there any meaningful difference between using a find_by helper or not? Are there any reasons I'm overlooking for opting for shorter lines of code when doing things like this? Booking.find_all_by_user_id(1, :joins => :confirmation) Booking.find(:all, :joins => :confirmation, :conditions => [ 'bookings.user_id...

User specified dynamic model fields in Rails

Does anyone know of a gem or a good implementation of allowing the user to add fields to a model? Ex. User would like to add a "internal notes" field to the contact model. In the interface they would just select "New field" > "Type: Text" Thanks ...

rails union hack, how to pull two different queries together

I have a query which searches two separate fields in the same table... looking for locations which are most likely a specific city, but could also be a country... ie the need for two fields. Table looks like: Country City Germany Aachen USA Amarillo USA Austin Result: Keyword Sideinfo Aachen Germany USA ...

How to turn off auto_increment in Rails Active Record

Is it possible to create primary key without auto_increment flag in ActiveRecord? I can't do create table :blah, :id => false because I want to have primary key index on the column. I looked up documentation but didn't find anything useful. Is it possible to create primary key without auto_increment? ...

Implement find(), save() ActiveRecord methods for ActiveMerchant CreditCard object

What's the best way to implement ActiveRecord's find() and save() methods for an ActiveMerchant ActiveMerchant::Billing::CreditCard object in a Ruby on Rails application? I'd like my credit card objects to inherit from ActiveRecord and ActiveMerchant. Caveat: I understand that saving credit card information to a database is always con...

order by foreign key in activerecord

I have a tables Foo and Bar. Foo has one Bar. When I query Foo, how can I order it by a date column in the Bar table? Thanks ...

Understanding Ruby on Rails ActiveRecord model Accessors

Hey there, I'm currently learning RoR and I've got the following problem: My Model, "DataFile", has a bunch of fields which I'd like to set from outside the Model, e.g. file = DataFile.new file.owner = 123 Now, as far as I know, I'd have to place an "attr_accessor :field" in my model, for every field that I'd like to modify from ou...

Validating length of habtm association without saving.

I have a user model with a HABTM relationship to groups. I do not want a user to be able to be in more than 5 groups, so would like to validate the length of the HABTM relationship. On the edit user page I have a list of checkboxes where the user can select the groups they want to be in (I'm using formtastic for the form). In my users...

writing an attribute to my through table with validation ???

hi all I have been trying to do this for ages and can seem to grasp it. hope someone can help ? i have a 'message' model that has many through 'distribute' relationship to a 'contact_detail' model. basically a message can have many contacts associated with it and a contact can have many messages. I can get this to work and save it ...

Is validates_presence_of necessary of using validates_length_of?

In an ActiveRecord model, is it considered best practice/necessary to use validates_presence_of when also using validates_length_of? For example: class Company < ActiveRecord::Base validates_presence_of :name validates_length_of :name, :in => 5..30 end To me, it seems redundant. Having a length between 5 and 30 means that the a...

performance/ruby/rails/db question

I'm creating an online bookmaker odds comparison site for soccer and I'm wondering how to calculate the best odds in Ruby/Rails. I have two models: fixture and odds Fixture has home and away teams, and odds model has bookmaker ID, home odds, draw odds and away odds. I have selections which just stores the selected fixtures/teams in th...

How do you prevent database changes inside a Rails ActiveRecord before_create filter from getting rolled back when it returns false?

I've added a before_create filter to one of my Rails ActiveRecord models and inside that filter I'm doing some database updates. Sometimes I return false from the filter to prevent the creation of the target model, but that is causing all the other database changes I made (while inside the filter) to get rolled back. How can I preven...

ActiveRecord named_scope, .scopes

The background to this problem is quite complex and convoluted, and as I am looking for a simple answer, I will leave it by the wayside in explaining my problem, and instead provide this hypothetical situation. If I have a simple ActiveRecord model called Automobile, with named_scopes like below: named_scope :classic, :conditions => { ...

How to implement Active Record inheritance in Ruby on Rails?

How to implement inheritance with active records? For example, I want a class Animal, class Dog, and class Cat. How would the model and the database table mapping be? ...

find all children of an object tagged with acts_as_taggable_on

I have a rails site with two models: events and dates events has many dates events are tagged using acts_as_taggable_on I'm trying to find all dates (chronological order) for events tagged with a specific tag. I can figure out how to get all the tagged events, but I can't figure out how to get all of the dates. The end result I'm ...

Rails ActiveRecord w/ MySQL Hangs Once Connection Pool Limit is Reached

Unless specified, ActiveRecord fires up 4 default connections. I've noticed if I reload a simple page, which makes at least one ActiveRecord call, 4 times every thing functions normal. However, on the fifth time, it hangs for almost 5 seconds. So, every 5th page load it hangs for 5 seconds. I upped the default connection pool in my ada...

Associating two records after create in Rails

I'm working on an association between two models: class Person < ActiveRecord::Base belongs_to :user end class User < ActiveRecord::Base has_one :person end Many person records exist in the system that don't necessarily correspond to a user, but when creating a user you need to either create a new person record or associate to an...

Silblings in has_many relationship

A user has many employments. What do you think? Is this a valid and clear way to fetch all siblings (belonging to the same user) of a given employment object? class Employment < ActiveRecord::Base belongs_to :user has_many :silblings, :primary_key => :user_id, :foreign_key => :user_id, :class_name => 'Employment' end...