activerecord

Is it important to deal with query caching in ActiveRecord unit tests?

In the Hibernate world, you can often have unit tests that appear to pass but there are actually bugs that don't show up because you're dealing with cached data. For example, you may save a parent with its children thinking it's cascading the save. If you re-query for the parent after the save and test the size of the child collection,...

Ruby on Rails Model.find generates a MySQL error

I have Product model and it has many categories with a has_many :through association In my controller I am trying to do a find by with categories.category but it comes up with a mysql error. Model.find(:all, :conditions => ['categories.category_id = ?', @category.id ]) Ideas? ...

Rails Nested editing form for self-referential HABTM Active Record Object

OK, this one is a doozy. I have an ActiveRecord object that, among other things, includes a relationships as follows: class Sample < ActiveRecord::Base has_and_belongs_to_many :related_samples, :class_name => "Sample", :join_table => "related_samples", :for...

How to perform a join with CodeIgniter's Active Record class on a multicolumn key?

I've been able to make this code work using CodeIgniter's db->query as follows: $sql = 'SELECT mapping_code,zone_name,installation_name FROM installations,appearances,zones WHERE installations.installation_id = zones.installation_fk_id AND appearances.installation_fk_id = installations.installation_id AND appearances.zo...

XML Schema (XSD) to Rails ActiveRecord Mapping?

I'm looking for a way to convert an XML Schema definition file into an ActiveRecord modeled database. Does anyone know of a tool that happens to do this? So far the best way I've found is to first load the XSD into an RDBMS like postgres or mysql and then have rails connect to do a rake db:schema:dump. This however, only leaves me wit...

How to keep track of model history with mapping table in Ruby on Rails?

dream I'd like to keep record of when a user changes their address. This way, when an order is placed, it will always be able to reference the user address that was used at the time of order placement. possible schema users ( id username email ... ) user_addresses ( id label line_1 line_2 city state zip ... )...

How to Generate Castle ActiveRecord C# Classes for an Existing Database

What is the best way to generate C# classes for use with Castle ActiveRecord and NHibernate given an existing database structure? Can you recommend any of the class generation tools or is it just less hassle to write the classes by hand? ...

Rails callbacks (after_create)

Hey. I would like ActiveRecord to et some DB field automatically using callbacks. class Product < ActiveRecord::Base after_create :set_locale def set_locale self.locale = I18n.locale end end In ./script/console I do p = Product.create p Field p.locale is not set. What did I do wrong? Thx for help! ...

Rake task which consumes many data freezes

Hi all, I have a simple rake task which consumes pretty much data via ActiveRecord. (contacts has ~47k rows) Contact.all.each do |contact| contact.update_attribute ... end When I run that task ~400 rows get updated and then the task stucks. No errors and no database activity at all... How do I make this work properly? ...

Rails extending ActiveRecord::Base

I've done some reading about how to extend ActiveRecord:Base class so my models would have some special methods. What is the easy way to extend it (step by step tutorial). Thx! ...

Saving nonprintable characters to the database in Rails

I am unable to save nonprintable characters (e.g. "\x83") to the database using Rails (v2.2). Simplified example (from the console): >> f = FileSpace.create( { :name => "/tmp/\x83" } ) => #<FileSpace id: 7, name: "/tmp/\203"> >> f.name => "/tmp/\203" >> FileSpace.last => #<FileSpace id: 7, name: "/tmp/"> So you can see, Rails is sile...

Rails ActiveRecord conditions

Is there a way to create a condition like this? @products = Product.find(:all, :limit => 5, :conditions => { :products => { :locale => 'en', :id NOT '1' }, :tags => { :name => ['a','b']}) I would like to list all products not including product 1. Thx. ...

rescue from ActiveRecord::RecordNotFound in Rails

Hello, A user can only edit its own post, so I use the following to check if a user can enter the edit form: def edit @post = Load.find(:first, :conditions => { :user_id => session[:user_id], :id => params[:id]}) rescue ActiveRecord::RecordNotFound flash[:notice] = "Wrong post it" redirect_to :action => 'index' end ...

Getting a rails model with statistics

I have a couple of models in a Rails app. Let's say it's Products and Sales. A product has many sales, and a sale belongs to a product. What I am looking to do is get a list of all products (possibly with conditions) and next to each product provide a count of the number of sales, similar to: Product 1 (10) Product 2 (22) Whilst...

ActiveRecord Claims No Adaptor Specified

When I try to run the following, I get an error back from ActiveRecord stating that the connector hasn't been found. require 'activerecord' ActiveRecord::Base.establish_connection( :adaptor => "sqlite3", :database => "db.sqlite3" ) Error Message: >> ActiveRecord::Base.establish_connection("adaptor" => "sqlite3-ruby") ActiveR...

How can I load some ActiveRecord models from a YAML file and save them to the DB?

I'm trying to save some lookup table data out to a YAML file so that later when I need to set up my app on a different machine I can load the data in as seed data. The data is stuff like select options, and it's pretty much set, so no worries about the live data changing between serializing and deserializing. I have output the data lik...

Ruby On Rails ActiveRecord model not showing "blacklist" field

So I'm trying to rough out a design in Ruby, and so I ran ruby script/generate scaffold item name:string description:text model:string manufacturers_name:string category:string weight:decimal upc:string ebay_cat_id:string blacklist:bool in_discovery:bool archived:bool The only problem is that none of the bool fields are on the model....

active record find from 2 tables in rails

Hello, I have two tables 'users' and 'loads' in the models I have defined that 'loads' belongs to a user and that a user have many loads Now when I want to show details for a load I use this in my controller: @load = Load.find(params[:id]) and then in my view output data like: <%=h @load.user_id %> <%=h @load.date %> . . . What I...

has_many :through association through two different associations

I have four model classes: class Group < ActiveRecord::Base has_many :projects has_many :personal_blogs end class Project < ActiveRecord::Base has_many :events, :as => :event_producer end class PersonalBlog < ActiveRecord::Base has_many :events, :as => :event_producer end class Event < ActiveRecord::Base belongs_to :event_p...

Ruby on Rails ActiveRecord: table with foreign keys in more than one other table

I'm new to Ruby on Rails and I'm trying to model my table relationships. To simplify the problem, let's say I have 3 tables: -- Customer (id, address_id, ...) -- Employee (id, address_id, ...) -- Address (id, ...) Would the Address model have the following? has_one :customer has_one :employee I know this is true in the case of a sin...