activerecord

Namespaced models in Rails: What's the state of the union?

Hi all, Since the beginning, Rails has had issues with namespaced models. As time went on, pretty much everybody gave up on using it. Myself included. With Rails 2.3 out, I'd like an update on the situation. Specifics questions I have in mind are: first off, is it good to go? table naming, what rule to follow? associations, how to d...

How do I return a Generic Type through a method in a base class?

I've been trying to create a simple base class that encapsulates some of my conventions for database access. I generally create a sproc named "products_retrieve_product" to select a product based on productID. I would like the method "Retrieve" in the base class to return the type that the derived class supplies in it's definition. I...

Using ActiveRecord, is there a way to get the old values of a record during after_update

Hey everyone, Setup using a simple example: I've got 1 table (Totals) that holds the sum of the 'amount' column of each record in a second table (Things). When a thing.amount gets updated, I'd like to simply add the difference between the old value and the new value to total.sum. Right now I'm subtracting self.amount during 'before_up...

Ruby Object#id warnings and Active Record

We keep seeing warnings like the following when we run our specs: Object#id will be deprecated; use Object#object_id The code in question is accessing the id of an ActiveRecord model (which is an attribute on the table, obviously, rather than the object instance ID in the Ruby VM). Does anyone know how to turn these particular war...

How do I control user assets, but let admins see everything?

I have a Rails app where Users have Memberships to Projects (and other things, polymorphically). Users also have Roles. I want User#projects to work like a normal ActiveRecord find, but I also want administrators to have access to every project. For a while I've been doing this: class User < ActiveRecord::Base has_many :memberships,...

Is there a java implementation of the ActiveRecord pattern that is built on top of Hibernate, similar to Castle Windsor?

I am looking for a Java implementationation of the ActiveRecord pattern which is built on top of Hibernate. In .Net there is a open source project Castle Windsor ActiveRecord which implements the ActiveRecord pattern on top of NHibernate. I'm looking for something like this, except sitting on top of the NHiberate persistence frameowork...

Ruby on Rails Join table relationship

Hello I am trying to use my join table "showing" to get a list of movies I was trying to use this code but it does not work. @showing_list = Showing.find_sorted_showings("time") @movie_list = @showing_list.movies <-- NoMethodError Here is my Showing class class Showing < ActiveRecord::Base belongs_to :movie def self.find_s...

rails : what's wrong with this multiple join with conditions on the associations?

Here are my models: class Deck < ActiveRecord::Base belongs_to :game has_many :deck_cards end class DeckCard < ActiveRecord::Base belongs_to :card belongs_to :deck end class Card < ActiveRecord::Base end Here's my attempted find: DeckCard.all :joins => [:card, :deck], :conditions => {{:decks => {:game_id => @game.id}}, {:ca...

With ActiveRecord has_many through relationships how do I delete associations while keeping objects

In Rails how do you delete a has-many through association while retaining the formerly associated objects? Is there an ActiveRecord way to do this or I do need to write the SQL? Also is it possible for the objects to remain friends once the relationship is gone? [ <-- lame joke attempt] ...

Only delete from memory not from database

Hi all, I have an ActiveRecord array containing a list of shops. shops = Shop.find(:all) I want to delete a record from shops without deleteting it from the database. shops.delete(a_shop) wuold result in a delete sql querry. I just want the shop to be deleted from the activerecord array but not the database. Can this be done? T...

How can I avoid running ActiveRecord callbacks?

I have some models that have after_save callbacks. Usually that's fine, but in some situations, like when creating development data, I want to save the models without having the callbacks run. Is there a simple way to do that? Something akin to... Person#save( :run_callbacks => false ) or Person#save_without_callbacks I looked in t...

Problem with adding custom sql to finder condition

I am trying to add the following custom sql to a finder condition and there is something not quite right.. I am not an sql expert but had this worked out with a friend who is..(yet they are not familiar with rubyonrails or activerecord or finder) status_search = "select p.* from policies p where exists ...

How to get the Join of Two One to Many associations?

I have two hibernate entities User and Blog. User can be interested in multiple Tags. Blog can belong to multiple Tags. For a User, How do i find the Blogs which belong to the Tags the User is interested in? I need something like Select * from Blog where Blog.Tags IN User.Tags except that SQL or HQL doesnt allow such comparisons in ...

Rails Active Recod table associations

Hello My question involves a few different active records class Respondent < ActiveRecord::Base has_many :phone_numbers, :dependent => :destroy has_many :email_addresses, :dependent => :destroy belongs_to :phone_number, :foreign_key=> "primary_phone_id" belongs_to :email_address, :foreign_key=> "primary_email_id" end class User < Resp...

how to access rails join model attributes when using has_many :through

I have a data model something like this: class CollectionItem < ActiveRecord::Base # the collection_items table has columns collection_item_id, collection_id, item_id, position, etc self.primary_key = 'collection_item_id' belongs_to :collection belongs_to :item end class Item < ActiveRecord::Base has_many :collection_items ...

What is causing this ActiveRecord::ReadOnlyRecord error?

This follows this prior question, which was answered. I actually discovered I could remove a join from that query, so now the working query is start_cards = DeckCard.find :all, :joins => [:card], :conditions => ["deck_cards.deck_id = ? and cards.start_card = ?", @game.deck.id, true] This appears to work. However, when I try to move th...

Best practices for Active Record Pattern and using static methods for group operations.

It is true what they say about design patterns that they are simply the embodiment of techniques already in general use. I have been using the Active Record Pattern since 1985. One of the attributes of this pattern is the use of static members in the implementation to perform searches that return collections of the underlying data. cl...

Eager loading last subordinate record

Suppose that model X has_many Ys and model Y belongs_to X. Each Y has a date when it has been inserted to the database. Now, is it possible to load all Xs and eager load last Y for each of X (only the last because each X has bazzilions of Ys)? ...

Find the associations for an ActiveRecord class at run-time?

I would like to find the assocations of an ActiveRecord class at runtime... Let's assume I have the following: class Person < ActiveRecord::Base has_many :chairs has_many :pens end class Chair < ActiveRecord::Base belongs_to :person end class Pen < ActiveRecord::Base belongs_to :person end How can I find out at runtime that...

Rails: How do I find() all records unique in certain fields?

I have a list of 'request' objects, each of which has fairly normal activerecord qualities. The requests table is related to the games table with a join table, 'games_requests,' so that a request has a request.games array. The question is, is there a way to do a find for the last n unique requests, where uniqueness is defined by the ga...