activerecord

Why does ActiveRecord has_many use delete_all instead of destroy_all?

I have a model which has many children. I was setting/removing the children as such: mymodel.children_ids = [1,2,3] mymodel.save #add the children mymodel.children_ids = [1] mymodel.save #remove children 2,3 This works just fine, but I just realized that none of the callbacks (i.e. after_destroy) are not being called on the children m...

How to map a database view using ActiveRecord?

Has anybody tried mapping database views in oracle using ActiveRecord? Please can I get some sample code for that? ...

Smells like ActiveRecord messes object attributes on save

I have got a bunch of records in locations table: ... *************************** 8. row *************************** id: 8 feed: http://feeds.bbc.co.uk/weather/feeds/rss/5day/world/4564.xml ... *************************** 11. row *************************** id: 11 feed: http://feeds.bbc.co.uk/weather/feeds/rs...

Simple model of blog post

I want to have a site that is a simple blog so I created a model: class Post < ActiveRecord::Base attr_accessible :title, :body end I want to use Markdown but without HTML tags. I also want always to keep database clean and my idea is to use before_save()/before_update() callbacks to sanitise my input and escape HTML. I do...

ActiveRecord Nested Includes producing SQL Error.

paginate :per_page => per_page, :page => page, :include => [{:stores => :locations}, :categories, :kosher], :origin => origin, :conditions => ["products.name LIKE ? #{conditions}", "%#{search}%"], :within => distance, :order => sort_order I am using ruby 1.9.1p243 with Rails 2.3.5 on OSX and this cod...

Finding the next ActiveRecord model object in the database using given object

I have an ActiveRecord model object @gallery which represents a row in the Galleries MYSQL table. Is there a way for me to ask @gallery to give me the id to the next gallery object in the table? The obvious way for me to do this is to : @galleries = Gallery.find(:all) index = @galleries.index(@gallery) @nextgallery = @galleries[index+...

Rails index of an association

I find myself requiring this. Assuming cart is a model which has a list of users. def index_of_item cart.users.each_with_index do |u, i| if u == current_user return i end end What's an easier way to get the index of an association like this? ...

Validates_uniqueness_of does not work when doing a large Transaction

I have a validate_uniqueness_of :field inside my ActiveRecord model. When i do a single create/update it works nicely but i have to do some large batch creation from csv files inside a Transaction When i am in the transaction the validate_uniqueness_of does not detect the error and the model is saved! ...

Updating an associated record from a join table model

I have a has_many :through relationship associating players to teams THROUGH managements. I want to have a counter on the teams table (a bit like a counter cache) that tells me how many new associations there have been since the beginning of the week. Of course a counter cache wont work because it will always give all the associations t...

Named_scope in rails unique records?

Is it possible to have named_scope return records unique for a certain column? e.g named_scope :unique_styles, :order =>"title desc", :limit => 3 That will give me three styles but what if I want to make sure the title is different? In tihs case there may be three records with the same style, I want this named_scope to only give uniqu...

What is the best way to get unique elements of an array of activerecord objects based on attributes of the object?

In my application I am trying to display only unique elements of an array of activerecord objects(loss_reports) based on two attributes of a loss_report. Schema class Agent < ActiveRecord::Base has_many :loss_reports, :through => policy end class LossReport < ActiveRecord::Base belongs_to :agent end I first tried to overr...

Activerecord Nested :include fails

I have an AR query using 'will_paginate' that looks like this: paginate :all, :page => criteria[:page], :per_page => criteria[:per_page], :include => { :user, :person }, :conditions => [conditions , criteria[:from_date], criteria[:to_date], criteria[:patient_id],criteri...

Rails Single Table Inheritance without "type" column

I'm porting some functionality to Rails, and I'm working with an existing table which is for comments. Basically, there are two types of comments - profile comments (photo_id column is null) and photo comments (photo_id column is set to photo's ID) I got single table inheritance working just fine by adding a type field to the table, bu...

single table inheritance: must all classes in hierarchy have same properties?

I have the following class Item < ActiveRecord::Base end class Talk < Item end with the migration class CreateItems < ActiveRecord::Migration def self.up create_table :items do |t| t.string :type t.string :name t.text :description t.time :start_time t.time :duration t.timestamps end en...

No gem called "activerecord-sqlite3-ruby-adapter"

I am trying to set up active records on top of a sqlite3 database with native ruby 1.8. This should work easily enough, I have seen plenty of examples out there that explain how. I am using some example code I have found, its pretty basic and starts with the following lines: require 'rubygems' require 'active_record' #require 'sqlite3-r...

Issues with displaying validation messages with Nested Forms (Rails 2.3)

I have a nested form, and for the most part it is working great. The only issue that I have is in displaying validiation errors. Consider the following object relationship: a Project has_many :tasks, a Task has_many :assignments if a validation error occurs on an assignment, with <%=project_form.error_messages %> It displays Task Ass...

Rails - render :action to target anchor tag?

I'm looking to use render like so: render :action => 'page#form' I also tried this: render :template => 'site/page#form' That didn't work either. The form on this particular page is at the very bottom, and if any errors occur on submission I'd hate for the user to be defaulted to the top of the page. I also need to use render (not ...

At what point is a simple dataset better stored in SQL Server than a CSV file when using ActiveRecord

I have a dataset consisting of roughly 10000 5 field records. I need to be able to retrieve a record based on the value in one field, which is a unique string. Each record will likely only be accessed once or twice. The application accessing the data uses Castle with ActiveRecord, backed onto SQL Server 2005. I recognize that at some p...

Subsonic Query Construction for Contains(Guid)

I have a "Notes" table. Notes support one level of threading - in other words you can reply to a note but cannot reply to another reply. So the table looks something like the following: CREATE TABLE [dbo].[Notes] ( [NoteId] [uniqueidentifier] ROWGUIDCOL NOT NULL DEFAULT (newid()) CONSTRAINT [PK__Notes] PRIMARY KEY ([NoteId]), [...

How to map 2 db columns to one property

Given a migration class CreateTalks < ActiveRecord::Migration def self.up create_table :talks do |t| t.integer :duration_hours t.integer :duration_minutes end end def self.down drop_table :talks end end and model class Talk < ActiveRecord::Base end and object class Duration attr_accessor :hours,...