activerecord

How to isolate/retrieve/count a subset of returned records in Rails

Hello all. I'm writing a reports dashboard for a rails app. The dashboard is for user data, and currently it's running multiple count an select queries to build the four or five reports on the page. I'm sure that there is a more efficient way to do this. How would I go about structuring the controller method so that it only runs one ...

Creating AR model for Drupal database

how do I change my activerecord model default behavior for the find method? For example, i want to search for all books inside drupal nodes database, but drupal uses only one table for all data, and uses the 'type' column to find out the type class Book < ActiveRecord::Base set_table_name 'node' def find(*args) :conditions => {...

Validate presence of nested attributes

How do I validate that a model has at least one associated model using nested attributes? This has been driving me crazy as I am sure that I am missing something simple. For example, I want to require that a List always has at least one Task. class List < ActiveRecord::Base has_many :tasks, :dependent => :destroy accepts_nested_at...

Execute SQL query from Rails when connecting to SQL Server 2005 through activerecord-sqlserver-adapter

So I'm connecting to SQL Server 2005 from my Rails app via the activerecord-sqlserver-adapter. I'm able to execute stored procs by doing Mymodel.execute_procedure("thisProcedure", param1, param2) But I have a stored proc that has a SQL INOUT variable that I'm having trouble with. I execute it, and I'm not seeing that variable returne...

Using dynamic finders to specify NOT NULL

I very often want to use dynamic finders to specify NOT NULL. So… this works: Widget.find_all_by_color('blue') this works: Widget.find_all_by_color(nil) But how can I do SELECT * FROM `widgets` WHERE `color` IS NOT NULL; ? ...

Why is :if not being recognised by ActiveRecord validations?

I have a problem where an :if clause within an ActiveRecord validation is not being honoured. My model has an ip_port attribute which I am validating as being present, numeric and within a certain range. I am trying to ensure that each condition only generates one error. I do not want the situation where an empty attribute results in th...

interesting rails association challenge

I have stumbled on an interesting challenge regarding active record associations: I have an Account model which can have multiple and different organisations attached to it (like for example a Company, a Contractor, a Person) and it also has a different role with each association (accountant, owner, viewer, etc.). So I am not sure what...

Ruby activerecord seems to ignore set_table_name

I'm running ruby on Mac OSX 10.6.4, using Ruby 1.8 and activerecord 2.3.8. I'm running the following code inside of NetBeans 6.9 but I'm using the ruby interpreter from 10.6 I have the following code: require 'rubygems' require 'active_record' ActiveRecord::Base.establish_connection ( :adapter=> "mysql", :host => "localhost",...

Unknown Attributes on nested form in Rails

Hi all, I'm having trouble getting my InventoryItem to accept nested attributes which is strange. In my script/console, I did the following: >> InventoryItem.create!(:name => 'what', :image_attributes => [ {:image => File.open("/home/davidc/Desktop/letterbx.jpg", "r") }]) ActiveRecord::UnknownAttributeError: unknown attribute: image_a...

Are there advantages to using foriegn key constraints when working in an active record framework like ruby-on-rails?

I'm moving back into full time web development after a 5 year hiatus. My previous experience (no active record or MVC) tells me to be very thorough with my database schema. Foreign key constraints, unique indexes, etc... can really help out when your writing spaghetti code. Does the community still find these useful when working in an...

after_update callback issues

I'm trying to recalculate percentages in an after_update callback of my model. def update_percentages if self.likes_changed? or self.dislikes_changed? total = self.likes + self.dislikes self.likes_percent = (self.likes / total) * 100 self.dislikes_percent = (self.dislikes / total) * 100 self.save end ...

Rails single table inheritance and subclasses name problem

Hi, all. I have 3 classes: class User < ActiveRecord::Base has_many :events, :foreign_key => 'owner_id' end class Event < ActiveRecord::Base belongs_to :owner, :class_name => 'User', :foreign_key => 'owner_id' end class Event::User < Event end Class Event have 'type' field, and work as STI. Now i create Event: Event::User.cre...

Rails 2.3.5 and inconsistent JSON/XML ActiveRecord serialization

I'm finding that XML and JSON serialization in Rails 2.3.5 don't seem to work the same. Has anyone encountered this and come up with a workaround? class Record < ActiveRecord::Base ... def my_method { :attribute1 => 'value1', :attribute2 => 'value2' } end ... end @record.to_json(:method => [:my_method]) returns correct JSON...

How to convert class to inherited class while using ActiveRecord

I've got a class Project and a class Case, which inherits from Project. When having a list of Projects we might decide later on to make a Case of the selected Project. How can I achieve this? Project: [ActiveRecord (Table = "projects", Lazy = true), JoinedBase] public class Project : ActiveRecordValidationBase<Project> { priva...

MySQL get rows, but for any matching get the latest version

I'm developing a CMS, and implementing versioning by adding a new entry to the database with the current timestamp. My table is set up as follows: id | page | section | timestamp | content "Page" is the page being accessed, which is either the path to the page ($page_name below), or '/' (to indicate 'global' fields). "Section" is t...

MonoRail ActiveRecord/NHibernate calculation in where clause

Hey, I'm trying to get businesses within a certain number of miles of a user, using a formula to get the distance between the lat/long of the business and the lat/long of the user. Here is the code: var criteria = DetachedCriteria.For<Core.Models.Business>(); criteria.Add(Restrictions.Le(String.Format(@"(3959*acos(cos(radians({0})...

Create instance of Rails model with has_many association prepopulated

This is best explained by example. The following is simple to do: class Foo < ActiveRecord::Base has_many :bars end 1a>> foo = Foo.new => #<Foo id: nil> 2a>> foo.bars << Bar.new => [#<Bar id: nil, foo_id: nil>] 3a>> foo.bars => [#<Bar id: nil, foo_id: nil>] However, I want all Foo objects initialized with a Bar without having to ex...

ActiveRecord object serialization and deserialization to the database

Hello, I would like to deserialize an serialized object. So it's possible to process such as (with JSON): >> l = Yea.create(:title => "foo bar") => #<Yea id: 3, title: "foo bar", created_at: "2010-07-05 21:44:54", updated_at: "2010-07-05 21:44:54"> >> j = l.to_json => "{\"yea\":{\"created_at\":\"2010-07-05T21:44:54Z\",\"title\":\"foo ...

SubSonic Active Record Generate Class

hi everyone, i wanna use to subsonic 3.0.4 but i dont know, how can i start to generate class and use to new Template of T4. example: create new solution for generating to table's class and add to my project. ?? thanks ...

Heroku Postgresql error

I deployed an app that uses ActiveRecord to Heroku, and I got an internal server error. It works fine on my local machine (where the database is SQLite). Below is the log message for the error. The newsletters table is just a table with no associations. It's got just one field for email addresses. I don't know PostgreSQL, and I'm not sur...