activerecord

Ruby on Rails ActiveRecord: pluralization

I'm new to Rails, so forgive my ignorance of ActiveRecord. One of my models is named "campus". I ran the migration and it pluralized everything except "campus". I thought that was lame so I added the code to the environment config to leave everything singular. I deleted the tables, manually edited the migration files to use singu...

Rails belongs_to testing

I have a Proposal model that belongs to Project: class Proposal < ActiveRecord::Base belongs_to :project has_many :articles, :as => :document, :dependent => :destroy has_many :sections, :through => :articles # proposal has project - test/unit/proposal_test.rb validates_presence_of :project_id end The route I set up to show ...

Including calculations on related data in an ActiveRecord query

Let's say I have some models: User, Post, and Vote. A user has many posts and a post has many votes. Votes can be either an up vote or a down vote (stored as a boolean). What I am considering is the best way to do queries like these: All users, and the total number of up votes they've received. All users, and the post that has received...

Active Record: ordering finds and also returning nulls

I want to find a ordered list of runners by their results. models class Race < ActiveRecord::Base has_many :runners, :dependent => :destroy end class Runner < ActiveRecord::Base belongs_to :race has_one :result, :dependent => :destroy end class Result < ActiveRecord::Base belongs_to :runner end trying to use something like t...

Stored procedure, activerecord, and alternatives?

In a PowerBuilder-based project, there are overs three hundred stored procedures on a Microsoft SQL Server. It's a client-server application which relies a lot on PB's DataWindow. Now, there is an feature request from the users which will most likely add several web-based screens to interface with the system. We are evaluating Rails (f...

Can ActiveRecord create tables outside of a migration?

I am working on a non Rails web app, so no migrations script by default. The Sequel ORM lets me create tables easily in a script: #!/usr/bin/env ruby require 'rubygems' require 'sequel' ## Connect to the database DB = Sequel.sqlite('./ex1.db') unless DB.table_exists? :posts DB.create_table :posts do primary_key :id varcha...

Rails - Changing the parent of a child and ensuring callbacks are executed

If I have class Parent < ... has_many :children, :before_add => :prepare_baby_room :after_remove => :plan_holiday end class Child < ... belongs_to :parent :after_create => :gurgle_a_lot :after_remove => :cry end and I want to re-associate a child with a different parent, what's the cleanest way to do it whilst ensu...

Ruby 1.8.6 LocalJumpError when dealing with ActiveRecord

I have the following code: Tag.find_all_by_company_id(4).each.collect{|tag| tag.name }.join(",") (Essentially I'm trying to build a JS array of tag names) When I run this code, I get: LocalJumpError: no block given from (irb):13:in `each' from (irb):13 Any ideas? ...

Query (M:N) ordered by relationship attribute

Hi folks, I have a simple many-to-many E-R described as below: Model order.rb: class Order < ActiveRecord::Base has_many :cronologies has_many :statuses, :through => :cronologies end Model cronology.rb: class Cronology < ActiveRecord::Base belongs_to :order belongs_to :status validates_uniqueness_of :order_id, :scope => :...

Why am I getting nil for a belongs_to relationship in Rails?

I'm having a problem in my Ruby on Rails app with a model where a belongs_to relationship keeps ending up being nil. Given the following models: class Chassis < ActiveRecord::Base belongs_to :model belongs_to :chassis_size end class Model < ActiveRecord::Base has_many :chassis end class ChassisSize < ActiveRecord::Base...

Data integrity when updating/incrementing values in Rails

I am working on an application in which one module accesses a log (an ActiveRecord model) to increment various values at several points of the execution. The problem is that the script is likely to take several seconds, and other instances of the same script is likely to run at the same time. What I'm seeing is that while the script fin...

Share methods between named scopes

I have a bunch of named scopes and have a method within one of them that I would like to share between the other named scopes. I've sort of accomplished this by using define_method and a lambda. However, there is still some repeated code and I'm wondering is there a better approach? Here's a simplified example of what I've got. Assume I...

How to create a full Audit log in Rails for every table?

We recently began a compliance push at our company and are required to keep a full history of changes to our data which is currently managed in a Rails application. We've been given the OK to simply push something descriptive for every action to a log file, which is a fairly unobtrusive way to go. My inclination is to do something like...

Rails ActiveRecord date between

I need to query comments made in one day. The field is part of the standard timestamps, is created_at. The selected date is coming from a date_select. How can I use ActiveRecord to do that? I need somthing like: "SELECT * FROM comments WHERE created_at BETWEEN '2010-02-03 00:00:00' AND '2010-02-03 23:59:59'" ...

Can one automatically write a Rails migration for a table change via command line?

Is there a way to write this in the command line that will auto-generate the migration script below? Example: ruby script/generate migration renamePostsTableToActicles Generates: class RenamePostsTableToActicles < ActiveRecord:Migration def self.up rename_table :posts, :articles end def self.down rename_table :articles,...

Ruby on Rails: Is it possible to :include the other leg of a circular join table?

I'm working on an application that models friendships between users. class User has_many :friendships has_many :friends, :through => :friendships, :conditions => "status = #{Friendship::FULL}" end class Friendship belongs_to :user belongs_to :friend, :class_name => "User", :foreign_key => "friend_id" end ...

Ruby on Rails: Is there a way to find with ActiveRecord that will first try the cache and if it fails run a query?

Let's say that I'm looking for a specific item in a has_many association. Is there a way to find that won't rehit the database if what I'm requesting is already cached? Here's my situation (which is not working and does not do what I requested): class User has_many :friendships def some_function(buddy) f = friendships.detect{...

How to get 'è' (and not 'e') with activerecord and ruby 1.8.7

Hello folks, I am writing a simple script to update a table data. I am unable to get a record trough a field named "Agliè"; the problem is "è". c = Comune.find_by_denominazione_italiano_tedesco('Agliè') I realised that the problem can be patched using "Aglie", but I need to preserve the accent difference (these are town names, some a...

How to create Rails models with multiple complex associations/joins?

I am trying to figure out how to create ActiveRecord models with associations that can yield the same results as this SQL query: SELECT login, first_name, last_name, email_address FROM accounts INNER JOIN people ON person.id = accounts.person_id INNER JOIN email_address_people ON person.id = email_address_people.person_id INNER JOIN ...

Ruby on Rails: Nested named scopes

Is there any way to nest named scopes inside of each other from different models? Example: class Company has_many :employees named_scope :with_employees, :include => :employees end class Employee belongs_to :company belongs_to :spouse named_scope :with_spouse, :include => :spouse end class Spouse has_one :employee end Is ...