activerecord

Rails 2.3 session

Hi, I am developing a rails 2.3.2 app. I need to keep session_id for an order record, retrieve it and finally delete the session_id when the order is completed. It worked when I used cookies as session store but it doesn't for active_record store. (I restarted my browser, so no cache issue.) I know rails 2.3 implements lazy session loa...

ActiveRecord and the Validate function

I want to know why I need to clear the (ActiveRecord) errorlist myself after Validate function have returned false once? I use the following logic: Show user UI for entering text Validate object Showing user error message, ("to long text") and goto Goto to step 1. Save object. After user have entered an text that does fit into the ...

rails accepts_nested_attributes_for :reject_if not working

I gave on trying to override the autosave parameter since I think it can't be done. I moved the has_shipping_address from the Order to the ShippingAddress model and now I have: #the models.. class Order < ActiveRecord::Base belongs_to :billing_address belongs_to :shipping_address accepts_nested_attributes_for :billing_address ...

Is it meaningful to override the '==' method in ActiveRecord subclasses?

Rails' ActiveRecord::Base class defines an == method that returns true if the objects are identical or they have the same ID. I've overridden == in a couple of my Rails models to allow for more meaningful equality conditions. These work when I compare objects directly (e.g., through script/console), but if I do something like my_array_o...

Trying to define named_scopes and other ActiveRecord relationships on all models.

I'm trying to define a named_scope for all my models in a Rails application. Presently, I've been able to get close to this by writing an initializer for ActiveRecord::Base and putting regular methods in there. Of course, this offers no real advantage when it comes to creating query chains and is probably the least rails-ey way of getti...

Custom Named Attributes in Rails

Is it possible in ActiveRecord to customize/override the name of an attribute so that it does not match the column name in the database? My specific case involves a legacy column, "revision", that I can't remove at this time. The column name conflicts with acts_as_audited. Which of course errors out the legacy code that I need until my ...

has_many and a legacy scheme

I'm looking for advice here: I have two tables in legacy scheme, A and B, joined on A.somename = B.othername. Both of those columns are strings. So how do I set up relations between them in rails (v2.1.0)? Given that A has many B's, what would be the best practice: use :finder_sql and just write a SQL select, configure the relation th...

Index for multiple columns in ActiveRecord

In ActiveRecord there are two ways to declare indexes for multiple columns: add_index :classifications, [:species, :family, :trivial_names] add_index :classifications, :species add_index :classifications, :family add_index :classifications, :trivial_names Is there any difference between the first approach and the second one? If so, wh...

Rails model that joins through a join table

Is this possible? In an events system an event can have multiple times. (ie, if it is a 3 day event and each day it's at a different time). Each time has a place associated with it. Finally, each place has an address associated with it. Now, can I reference those addresses through my event model? I'm thinking something conceptually lik...

Constricting find conditions based upon runtime contexts

Wow crazy title! But here's the problem. In efforts to dry up my school website management system application, I've made a module of my application (FileSet) restful and placed it in one place. Previously FileSet was used in the general website system, and also in a kids learning area system. They both behaved exactly the same except for...

Setting the scope on a model for the rest of a request

I'm wondering if there is a way to set a scope on a model class for the rest of a request? I.e. I want to scope down some results, but I want to do it without the main restful controller knowing (perhaps in a before_filter injected into the controller). Contacts.scope = { :conditions => {:public => true} } if ladeda then later on Con...

Activerecord, select on string compare

Hi all, I have a table called Basic , start_time is one field with type :VARCHAR(5), which actually stores a 5 bytes time data: byte 0 and 1 map to the year , month and day, and byte 2 to 4 map to the hour, min and second. So, it could possible bytes 2 ,3 ,4 are all 0. And I want to do following query : Basic.find (:all , :conditions =...

Contest ranking question - how to rank entries in multiple categories?

I'm currently developing a video contest web application using Ruby on Rails. It integrates closely with YouTube, which it uses for submitting videos, comments, average rating, and popularity stats. The application will also count Twitter and (possibly) Facebook mentions, and count the number of times visitors have clicked an "Add This" ...

Rails unit testing gone completely wrong

I don't know what and when happend on my code but I got hundereds similar erros (not failures) of this : NameError: uninitialized constant ActiveSupport::Callbacks::Callback::NORMAL And my tests function just go useless now, as even I put something like: should "failed" do assert false end It still returns passed, any idea ? ...

math functions in active record sql queries? (pow, sqrt etc)

Active Record is not recognizing common SQL functions like POW and SQRT in the "find" method or "find_by_sql." How do I work around this? There seems to be no literature out there :-( ...

How to use long id in Rails applications?

How can I change the (default) type for ActiveRecord's IDs? int is not long enough, I would prefer long. I was surprised that there is no :long for the migrations - does one just use some decimal? ...

How can I change the connection being used by Castle ActiveRecord

I have an application build using ActiveRecord for which there is a new requirement to allow the user to select a database they wish to operate at various times in the course of the running of the application. The database connection is set up in the ActiveRecordStart.Initalize call. I would like to be able to change this connection whe...

Rails eager loading, possible bug

In my Rails 2.3.2 app I have 2 models: class Post has_many :approved_comments, :class_name => 'Comment', :conditions => ['approved => ?', true] end class Comment belongs_to :post end For some reason when I try to eager load my comments, I get an error post = Post.find(:first, :conditions => ["permalink=?", permalink], :includ...

Defining SubSonic 3 ActiveRecord migrations

I'm starting an ASP.NET MVC project using SubSonic 3 ActiveRecord. I added a table Users with a primary key ID and recompiled T4 files to generate User class. I want to make sure that, as I go along with the development, I can regenerate/migrate the database at any point. It looks like I have to create tables and relationships in the da...

Nil object on after_destroy

I have a method which updates a count of dependent objects for a parent class whenever a dependent is created or destroyed. This generally works, but for some reason when a parent is deleted by a third class with :dependent => :destroy, I get a nil error for the parent object when the count method is called and thus nothing gets deleted...