activerecord

Rails User model has_many activities (observer) but should also be observed

I'm running into an issue with an existing ActiveRecord::Observer model that records various Activities of a User the site. Everything was working really well, until I tried to observe the User class with the same Activity model that it uses to observe other models. Consider that: class Activity < ActiveRecord::Base belongs_to :user b...

How to manage non-autoincrement primary key in Rails?

I have lots of situations where I'd like to have a non-autoincrement primary key when using Rails. Example: I have one-to-one relationship between A and B. B describes some specific features added to A thus can't exist without A. So we have: A has one B B belongs to A The natural thinking would be having B.A_id as primary key. So I ...

Rails model: "has many" *simple* attribute

Let's assume this model: Movie - Title: String - Has many: - Alternative Title: String My questions is, how should I store the alt. title attribute? I am deciding between three approaches: Separate AR model: probably an overkill CSV in a signle DB column Serialized array in single DB column The latter two seems logically equi...

Question about Ruby on Rails, Constants, belongs_to & Database Optimization/Performance

I've developed a web based point of sale system for one of my clients in Ruby on Rails with MySQL backend. These guys are growing so fast that they are ringing close to 10,000 transactions per day corporate-wide. For this question, I will use the transactions table as an example. Currently, I store the transactions.status as a string ...

Cascade delete in Ruby ActiveRecord models?

I was following the screencast on rubyonrails.org (creating the blog). I have following models: comment.rb class Comment < ActiveRecord::Base belongs_to :post validates_presence_of :body # I added this end post.rb class Post < ActiveRecord::Base validates_presence_of :body, :title has_many :comments end Relations ...

What's a nice way to verify within a unit test that an ActiveRecord transaction is being used?

I have a class which performs several database operations, and I want to write a unit test which verifies that these operations are all performed within a transaction. What's a nice clean way to do that? Here's some sample code illustrating the class I'm testing: class StructureUpdater def initialize(structure) @structure = struc...

ActiveRecord: filtering children without hitting the database

I'm looking for a clean way to filter the children of a parent in a has_many relationship without hitting the database and thus reloading the db's view of the objects back into the app. For example: class Parent < ActiveRecord::Base has_many :children, ... ... end My understanding (correct me if I'm wrong) is that parent.childr...

Complex joins with legacy tables in Rails

Hello, I'm writing fairly simple web interface for invoice/customer/contract database. I would like to have the following structure for my models: class Invoice < ActiveRecord::Base set_table_name 't10_invoices' set_primary_key 'id_invoice' has_one :client end class Client < ActiveRecord::Base set_table_name 't20_clients' ...

How can I delete an entry from a HABTM join table in rails?

Through many iterations of testing, I just noticed that my join table that represents a HABTM relationship between two models isn't removing entries when instances of these models get deleted. Do I need to do something special when removing an instance of a model that has HABTM relationships? ...

How to get related object in Subsonic 3 (ActiveRecord)?

Given the following: class Customer string name end class Order int number end I am creating a list of orders. For each order listed, i'd like to display the associated customer name. is there a way to do that via the object graph, or do I have to look it up manually? Chris ...

Rails association errors possibly related to typus

I have a problem with associations. As a newbie to RoR, I've learned about associations from the guide on the RoR site. I have followed one of the example almost to the letter, the only thing being changed are the class names. The example being the following: class Document < ActiveRecord::Base has_many :sections has_many :pa...

Ruby, ActiveRecord and Legacy data that needs to be cast from char to integer on the fly

I have a legacy database that has integer data stored as chars that I would like to treat as numerical. Is there a quick and easy way to convert this is one place? The values are from 0-5 and it defaults to 0 so there are no nulls. I can't just fix the data because the legacy db will still be updated by the legacy app. So I am basica...

Hibernate Criteria Query - How to search through many-to-many with attributes?

I'm trying to create a Criteria query to select objects that are related via an association table. Insurer * - 1 Insurer_Section 1 - * Section InsurerSection has an association attribute: active:bool. How do I get all Insurers whose active attribute in the InsurerSection class is set to true? PS: I can't go like this: Insurer.FindA...

hql get objects where objects' property is "substring" of input value.

I am running into similar situation as this post. My pseudo code is string hql = "select ai from AreaInfo as ai where ai.PhoneSegment is substring of :InputPhoneNumber"; Using like wouldn't do the trick because what Like is doing is string hql = "select ai from AreaInfo as ai where :InputPhoneNumber is substring of ai.PhoneSegment"...

Show ActiveRecord objects like table in ./script/console

How to display ActiveRecords like >> Role.all +----+-----------+-------------------------+-------------------------+ | id | name | created_at | updated_at | +----+-----------+-------------------------+-------------------------+ | 1 | Admin | 2009-11-16 21:22:59 UTC | 2009-11-16 21:22:59 UTC ...

Rails add custom eager load

I have a number of custom find_by_sql queries in Rails. I would like to use eager loading with them but there doesn't seem to be a good way to do this. I have seen the eager_custom.rb file floating around and it doesn't seem to work with Rails now. It appear Rails does eager loading differently now, using 2 queries (the regular query ...

Handling Complex WHERE clauses with a PHP Query Builder

There are several ActiveRecord styled query builder libraries out there. Some are stand alone and some come built into frameworks. However, they really have trouble with WHERE and HAVING clauses when it comes to complex SQL. Setting other databases aside - I am trying to come up with a MySQL and PostgreSQL compatible WHERE() method that ...

Invalid source reflection macro :has_many :through

I have such angry associations: financings >- events >- subprograms >- programs. I want to get acces to last_financings from programs through all of them so code is: class Fcp < Program has_many :fcp_subprograms, :foreign_key => 'parent_id' has_many :subprogram_last_actual_financings, :through => :fcp_subprogra...

activerecord_session_store vs. sql_session_store

Does anyone have more recent stats on the speed gains in SqlSessionStore over ActiveRecord Session Store? Is anyone out there using SqlSessionStore because of gains over ARStore? More of a curiosity I guess. Seems there isn't a lot new on the SqlSessionStore side since like '07, even though the github.com repos show updates as late as ...

how to specify multiple relationships between models in rails using ActiveRecord associations

Say there're two models : User and Post, i want to keep track of who has read which post, and who writes which post, which user favorites which posts etc. Then i come up with the following solution: class User < ActiveRecord::Base has_many :writings has_many :posts, :through => :writings has_many :readings has_many :posts, :thr...