activerecord

How to combine ActiveRecord objects?

I'm wondering if there's an efficient way to combine the results of multiple ActiveRecord objects in Rails. For example, I might make three individual calls to three individual tables, and I want the results combined, and sorted by a common column. Here's a super basic code example that will hopefully make my question easier to understa...

Changing the column name of a rails object

What if I want to refer to a column of an ActiveRecord object by a different name? for example, I might call: @posts = Posts.find(:all, :select => "created_on") But instead of referring to @posts["created_on"], I'd like to refer to it as @posts["date"]. What would you recommend? Thanks! ...

How to insert predictable IDs for testing using ActiveRecord

I am trying to do some Cucumber testing like the following: Given I am logged in as admin When I go to the article page Then I should see "Edit Article" And I should see "Article Title" where the path to the article page is "articles/1" or some known id.The problem is that when I insert my data using my step definition Article.create...

Recreating this custom query using has_many

I'm hoping this will be an easy one :) I've been stuffing around for hours playing with the has_many options trying to emulate this: has_many :pages, :finder_sql => %q(SELECT * FROM `pages` LEFT OUTER JOIN `component_instances` ON `component_instances`.instance_id = `pages`.id AND `component_instances`.instance_type = 'Page' WHERE `comp...

Typecasting a custom column in rails ActiveRecord?

Let's say we do: default_scope :select => '*, 1+1 AS woah' in a model, we can then access woah as a method on the model, but it's a string. How do we typecast this so that it's an integer? In my real-world example I'm actually selecting an id from a joined table but it's being typed as a string. I need it to be a ruby integer. ...

SubSonic 3 ActiveRecord generated code with warnings

While using SubSonic 3 with ActiveRecord T4 templates, the generated code shows many warnings about CLS-compliance, unused items, and lack of GetHashCode() implementation. In order to avoid them, I did the following modifications: // Structs.tt [CLSCompliant(false)] // added public class <#=tbl.CleanN...

named_scope in rails with a has_many association

Hi all, I am trying to achieve what I think will be a fairly complex query using the magic of Rails without having lots of ugly looking SQL in the code. Since my database is dealing with rather specialised biomedical models I'll translate the following to a more real world scenario. I have a model Book that has_many :chapters and C...

(Rails, ActiveRecord) How do I use ActiveRecord calculations while still retrieving field names?

Hi All, Is there any way to use ActiveRecord calculations while also acquiring relevant field names? For example, I'd like to find the average age of all people with the various last names. Basically I'd like to use: Person.average(:age, :group_by => "last_name") ... as a substitute for "Select last_name, AVG(age) FROM Peopl...

How do I get the last SQL query performed by ActiveRecord in Ruby on Rails?

I'm looking for something like CodeIgniter's: $this->db->last_query(); (http://codeigniter.com/user_guide/database/helpers.html) ...

Subsonic 3 Active Record Testing

Using the new built in testing for Active Record (set "Test" as the connection string) I've hit a problem performing an update. The code I'm testing pulls an existing object out of the database, makes some changes to it, and then saves it. public void SaveProduct(string productId) { var dbProduct = Model.Generated.Product.SingleOrDefa...

Why does calling create as a class method not save the record?

Suppose a simple ActiveRecord model corresponding to a table with just two columns, id and name: class Model < ActiveRecord::Base def self.my_create(name) create(:name => name) end end If I call Model.my_create, a record is created and saved: >> Model.my_create('foo') => #<Model id: 1, name: "foo"> But If I call Model.creat...

How do I add parent instance specific information to a has_many relationship?

I am using the acts as taggable on steroids plugin and my web app has 4 models: Category Entry - acts as taggable and contains the following using single table inheritance Story Topic Category has two important fields: tags:string - A list of tags associated with this category primary_tag:string - A single tag that gets assigned ...

Writing an ActiveRecord plugin for Rails

I'm writing my first rails plugin and could use a little help. In a very simplified way, I'd like to do allow the developer to specify a value which I can count through a rake task. I'm thinking of something like this... class User < ActiveRecord::Base monitor "Users", count monitor "Active Users", count("activated_at != NULL")...

Ruby types of collections in ActiveRecord

If I have an object with a collection of child objects in ActiveRecord, i.e. class Foo < ActiveRecord::Base has_many :bars, ... end and I attempt to run Array's find method against that collection: foo_instance.bars.find { ... } I receive: ActiveRecord::RecordNotFound: Couldn't find Bar without an ID I assume this is because A...

Can a custom to_s method be created for a specific association collection?

I have an Entry model which has_many :tags. I want to be able to list my tags in an text input (ie. "tag-1, tag-2", etc.), however, I am running into an issue. If I just use form_for(:entry, form_options) do |f| f.text_field :tags end My text box gets created, but is filled with something like #<Tag:0xb79fb584>#<Tag:0xb79faddc>, w...

ActiveRecord: Find without associations

I have a many_to_many relationship between ImageShells and Users. I need to find all ImageShells that have no users. I have a query that finds them but how do I put this into a named_scope? SELECT * FROM image_shells INNER JOIN image_shells_users ON (image_shells_users.image_shell_id!=image_shells.id) class ImageShell < ActiveRec...

Generating sequential numbers in multi-user saas application

Hi! How do people generate auto_incrementing integers for a particular user in a typical saas application? For example, the invoice numbers for all the invoices for a particular user should be auto_incrementing and start from 1. The rails id field can't be used in this case, as it's shared amongst all the users. Off the top of my head...

Subsonic 3 Linq Projection Issue

OK I'm banging my head against a wall with this one ;-) Given tables in my database called Address, Customer and CustomerType, I want to display combined summary information about the customer so I create a query to join these two tables and retrieve a specified result. var customers = (from c in tblCustomer.All() ...

Ruby on Rails - Share one method with 2 models

I have the following module module SharedMethods # Class method module ClassMethods # # Remove white space from end of strings def remove_whitespace self.attributes.each do |key,value| if value.kind_of?(String) && !value.blank? write_attribute key, value.s...

How to get an array of table names in ruby

I'm trying to get the output of the query 'SHOW TABLES FROM database_name' into an array using the ActiveRecord database connection. I can't seem to figure out how to do this. can someone please enlighten me? -C ...