activerecord

Add readable field descriptions to ActiveRecord models

I'd like to add descriptions to ActiveRecord model fields to serve as basic instructions / examples for each of the fields. Basically model metadata. I can then display these in the UI (next to the fields on a form etc.) The way I'm planning to do it is simply create a static hashtable inside the model with the field name as the key and...

ActiveRecord counting with conditions over two tables

After watching, the latest RailsCasts with the include and joins information, I know I can do what I am doing in a much more efficient way. I have a very simple User model and a Status model. The User belongs to Status and the Status has many Users. I need to count how many users have a specific kind of status, this creates a new SQL cou...

Why do ActiveRecord callbacks require instance variables or instance methods to be prefixed with self keyword?

ActiveRecord has a few different callback methods used to simplify model logic. For example after_find and before_create methods. Consider this code example: class ExternalPrintingCard < ActiveRecord::Base belongs_to :user belongs_to :ph_user after_create :change_pin def change_pin self.user.randomize_printer_pin end ...

In ActiveRecord how do I use 'changed' (dirty) in a before_save callback?

I want to set my summary field to a sanitized version of the body field, but only if the user does not supply their own summary ie. params[:document][:summary] is blank. This appears to work fine if I create a new record, if I enter a summary it is saved, if I don't the body is used to generate the summary. However when I update the re...

SubSonic 3.0 ActiveRecord - Preferred approach building object graph?

Say I have a forum system with Threads, Posts and Tags. The structure is the same as StackOverflow: Threads have a 1-many relationship to Posts, and Tags have a many-many relationship to Threads. The (simplified) tables: Thread ------ ThreadID int PK Title varchar(200) Tag ---- TagID int PK Name varchar(50) ThreadTag ----------- T...

Setting up polymorphic associations in db when the super is a FK of subclasses?

Using class table inheritance it is recommended that the subclass refers to the superclass and not the other way around. Normally in Rails polymorphic associations work in the other direction--the super refers to the subclass. Recommended vehicle .id car .vehicle_id # PK instead of using id (identity column) boat .vehicle_id...

Is there a way to ensure one object reference per record in an ActiveRecord hierarchy?

It seems when I grab some hierarchical ActiveRecord structure that there are quite a few hits to the database. I improved this using the :include option to flesh out as much of the structure as possible. Even so, it seems that ActiveRecord does not map the reciprocal sides of a relationship (e.g. parent-child) with unique references to...

Castle - ActiveRecord - Inheritage

I'm trying to avoid creating the same properties in all ActiveRecord classes, so I am coding this: Have a base class where I have my common properties: Id, Version, LastUpdate, etc... public class IdentityBase<T> : ActiveRecordValidationBase<T> where T : class Then my "child" class would have his own properties and should inherit fro...

Rails ActiveRecord :joins with LEFT JOIN instead of INNER JOIN

I have this code User.find(:all, :limit => 10, :joins => :user_points, :select => "users.*, count(user_points.id)", :group => "user_points.user_id") which generates following sql SELECT users.*, count(user_points.id) FROM `users` INNER JOIN `user_points` ON user_points.user_id = users.id GROUP BY u...

Partial lazy loading

I've one object User which can have multiple Posts. Example: Load the user with lazy loading on the Posts IList<User> users = User.LoadAll() Then I want to read only "half" of the users[2].Posts[3] (retrieve only the attributes that I want and not all of them from that post object), is this possible to make? (Note, I do not want to u...

How can I use ActiveRecord on a database that has a column named 'valid'? (DangerousAttributeError)

I am accessing a database that I can't change and it has a column named valid defined. Anytime I try to access an attribute, I get this exception: valid? is defined by ActiveRecord (ActiveRecord::DangerousAttributeError) The exception makes sense, but since I'm not able to change the database, how can I get around this error? I tried ...

Showing join table field

Here I want to show the extra attribute CONFIRMED from the employments join table. What am I doing wrong? class Job < ActiveRecord::Base has_many :employments, :dependent => :destroy has_many :users, :through => :employments class User < ActiveRecord::Base has_many :employments has_many :jobs, :through => :employments class Empl...

Callback for changed ActiveRecord attributes?

I am aware of ActiveRecord::Dirty and the related methods, but I don't see a means by which I can subscribe to an attribute changed event. Something like: class Person < ActiveRecord::Base def attribute_changed(attribute_name, old_value, new_value) end #or attribute_changed do |attribute_name, old_value, new_value| end end ...

How can I create new records with has_many :through and honor :conditions?

Let's say I have a Course in which Students can enroll via a Membership (e.g. a has_and_belongs_to_many relationsip of Courses and Students). Some memberships are for students who are just observing the class (not for credit, etc.), so: class Course < ActiveRecord::Base has_many :memberships has_many :students, :through...

ORM that accepts SQL and simply maps the objects and relations?

I find that the use of ActiveRecord affects the way I design the database schema (though I wish it wouldn't). I'm thinking about the inefficiency of fetching data and how to reduce the overall number of queries. The find :include option can only get you so far. I come from writing stored procs that grab everything you need (for a part...

Why do migrations need the table block param?

Why does the ruby on rails migration syntax look like this: create_table :my_table do |t| t.integer :col t.integer :col2 t.integer :col3 end And not: create_table :my_table do integer :col integer :col2 integer :col3 end Personally I find the second snippet much more readable, are there any reasons w...

Manually instantiate ActiveRecord models and their relationships?

If I have T-SQL (or a stored proc) that returns records from multiple tables (using DBI perhaps), is there a way for me to manually instantiate the ActiveRecord models and their associations? Obviously, I’m after database performance here. I would like to be able to build my own object hierarchy (models and their relationships), but wh...

Rails' ActiveRecord serialize :attr method gives "Missing Class or module error"

Hi there, I'm trying to serialize a simple attribute in an ActiveRecord model, and Rails 2.3.4 doesn't like it. class Shopper serialize :tags end >> a = Shopper.new => <#Shopper...> >>a.tags = ['aoeu','stnh'] => ['aoeu','snth'] >> a.save => TypeError: class or module required anyone know what I'm missing? ...

How do I define ActiveRecord relationships between two models that relate to each other in two different ways?

In my app I have the classes User, Video, and Vote. Users and Videos can relate to each other in two different ways: as a one-to-many or as a many-to-many. The former is when a User submits a Video (one user can submit many videos). The latter is when a user votes on a video (users have many videos through votes, and vice versa). H...

Calculated field in RoR (act as an ActiveRecord method)

I am building an app for cognitive tests in Rails. I have a number of tests (Quiz objects) for my visitors. In the home page I want to show only quizzes that are ready for consumption: they must have a number of questions and a number of possible answers. Of course I could query with SQL, or create a class method retrying all objects whe...