activerecord

I need help figuring out the best way to query this in rails

I'm having a problem coming up with a query that would work for these models. I have three models that are connected. An Organization has many Users, and Users have many StatusEntries Basically this means I could do Organization.find(1).users.find(1).status_entries And have a list of status_entries returned to me. The problem is I ...

Why would association.build not assign parent_id to it child?

I've got the following models: In plan.rb has_many :tickets and in ticket.rb belongs_to :plan validates_presence_of :plan_id When executing the following code in the controller @plan.tickets.build( ... ) @plan.save save fails with error_message for ticket: plan can't be blank. (plan is valid.) ...

accessing children of children tables in Rails

I have a "tree-like" structure to my database in an app I am writing so that: training has_many class_times and class_time has_many reservations Is there a way to look up all reservations under a a given training? I could iterate through all of class times/add a foreign key, of course, but for some reason I have this little voice...

Basic Rails Question: One-to-One relationships

My comprehension of RoR is sadly lacking. I have three one-to-one relationships that I want to maintain in one view. I have the following models: class Ood< ActiveRecord::Base has_one :female_trait has_one :male_trait end class Female_Trait < ActiveRecord::Base belongs_to :ood end class Male_Trait < ActiveRecord::Base belon...

Codeigniter:$query->free_result() when using active record?

after i have finished with my result set from a active record query in a codeigniter model, should i use $query->free_result() or does activerecord automatically do that? ...

How to create activerecord style validations outside of activerecord?

Hi, I am working on a testing framework for the software that my company writes. Our product is web based and After I run a RESTful request I want to process the results. I want to be able to have activerecord type validations in each command class so that after it is run the results are automatically tested against all the "validation...

Complex associations in ActiveRecord models

I'm trying to understand how ActiveRecord deals with associations that are more complex than simple has_many, belongs_to, and so on. As an example, consider an application for recording music gigs. Each Gig has a Band, which has a Genre. Each Gig also has a Venue, which has a Region. In the rough notation of MS Access (which I'm sudd...

Find the Top 10 results from a table using Castle ActiveRecord

Hi, I'm trying to get say the top 10 scores for the users of my application. I'm usually using something along the lines of User.SlicedFindAll(0, 10, NHibernate.Expression.Expression.Eq("IsActive", true), NHibernate.Expression.Order.Desc("Score") which is usually used for pagination purposes. However, I don't want to add any const...

Infinity, ActiveRecord and MySQL: storage and comparison

I have user input strings specifying the cost of an event (eg: "$4 for non-members"). I am trying to parse out the upper (and lower) bound of the cost (in the example, upper == lower == 4.00). Suppose that a given string cannot be parsed (maybe it's blank). In this case, I want to be able to store in the database that the maximum cost ...

Is there a default english translation file for Active Record?

I am upgrading an application a rails application to 2.3.2 and I am finding that I can't display the default validation error messages for ActiveRecord because I don't have a translation file for it. This is the error that is reported: translation missing: en-US, activerecord, errors, template, header translation missing: en-US, active...

How can ActiveResource create HABTM relationships?

I am trying to add multiple HABTM relationships through ActiveResource, but I am running into a lot of trouble. It seems that in a traditional rails app (i.e. all ActiveRecord), you can simply set your HABTM ids, and do something as such: habtm = {:category_ids => [1,2,3]} article.update_attributes(habtm) Unfortunately, when I try to...

Contextual ActiveRecord Model filtering

Throughout our site there's a common model, let's say Video, that has a number of attributes like private/public/pending, downloadable, etc. Based on the controller, action, request parameters and current user, we want to filter out particular videos from display. For instance, on the homepage we only want to show videos that are public...

ActiveRecord Inheritance with Different Database Tables

I have just started investigating using more advanced models in Rails. One that I use regularly, with great success, is model where a many-to-many cross-reference relationship is accessed by a class that itself is a sub-class of the base class in the many-to-many relationship. This way the cross-reference class can act as a stand-in for...

How do I stop ActiveRecord looking for a table?

I am trying to create an ActiveRecord model called 'Search' without a table. I keep getting this error when I do @search = Search.new. sql::Error: Table 'searchdemo_development.tablelesses' doesn't exist: SELECT * FROM tablelesses I am using the idea from this comment: http://stackoverflow.com/questions/315850/rails-model-without-dat...

Find all objects with no associated has_many objects

In my online store, an order is ready to ship if it in the "authorized" state and doesn't already have any associated shipments. Right now I'm doing this: class Order < ActiveRecord::Base has_many :shipments, :dependent => :destroy def self.ready_to_ship unshipped_orders = Array.new Order.all(:conditions => 'state = "...

Can't define :joins conditions in has_many relationship ?

Hi, I have a relationship table : create_table "animal_friends", :force => true do |t| t.integer "animal_id" t.integer "animal_friend_id" t.datetime "created_at" t.datetime "updated_at" t.integer "status_id", :default => 1 end linking animals to others. Best way to retreive associations in SQL is : SE...

Faster/bulk activerecord creation

My application is storing location data from GPS inputs. When importing a GPX file, a user can have from 500 - 10,000 GPS datapoints. Right now, I have a model for each trackpoint. It's working great, but on insert it is SLOW. 30+ seconds for the 10,000 datapoints. Is there any better way of bulk inserting? All the time is spent ...

Active Record Pattern and WCF: How to ensure data access methods on clients are "missing"?

I'm new to the Active Record design pattern and usually use a Data Mapper type pattern. The project I'm currently working on seems to meet nicely with the requirements for using the Active Record pattern in that the domain is fairly small and the object and database schemas are quite similar (though not identical obviously). I plan on ...

Evaluating :dependent => :destroy

In Rails 2.2.2 (ruby 1.8.7-p72), I'd like to evaluate the impact of destroying an object before actually doing it. I.e. I would like to be able to generate a list of all objects that will be affected by :dependent => :destroy (via an object's associations). The real problem I'm trying to solve is to give a user a list of everything tha...

Shortcuts in ActiveRecord?

Are there any shortcuts in Rails' ActiveRecord that enables you to search by value of a field? For instance, let's say I have a 'user' who can be active or inactive. Is there a nice way of doing User.active? or do I need to do User.find_by_active(1) Does this also apply to fields that may have many different values, such as a state co...