activerecord

Batch Looping A Model With Ruby

I know of the find_in_batches method for ActiveRecord, but this doesn't allow me to set my :order or :limit. I am trying to loop through my data, and for every 6 items I want to wrap them in a <div>. I was trying to whole... <% i = 0 @media.each do |media| %> <% if i%6 %><div class="section"><% end %> [...] <% if i%6 %></div><% en...

Behind the scenes: How does an ORM "think"?

I'm interested in some of the design behind Rails ActiveRecord, Doctrine for PHP (and similar ORMs). How does an ORM manage to accomplish features like chained accessors and how deep are they typically expected to work? How does an ORM construct queries internally? How does an ORM manage the queries while sustaining the arbitrary natur...

Rails (ActiveRecord) many to many table

I have two models, Users and Groups. Each group can have many users and each user can be in many groups. I currently have something simple like: User: has_many :groups Group: has_many :users So I have a groups_users table which is just creating rows with group_id and user_id. I want to add another column to this, (which I h...

How to override activerecord's default attribute column associations?

I'm working on a legacy database that is complete non-sense. I have a table called movie that contains columns with names like c00, c01, c02 and so on. The table also uses non-standard primary_keys. So I've created a class called movie like this: class Movie < ActiveRecord::Base set_table_name "movie" set_primary_key "idMovie" ...

SQLAlchemy - Models - using dynamic fields - ActiveRecord

How close can I get to defining a model in SQLAlchemy like: class Person(Base): pass And just have it dynamically pick up the field names? anyway to get naming conventions to control the relationships between tables? I guess I'm looking for something similar to RoR's ActiveRecord but in Python. Not sure if this matters but I'll...

How do I use ActiveRecord to find unrelated records?

I have a many-to-many relationship set up through a join model. Essentially, I allow people to express interests in activities. class Activity < ActiveRecord::Base has_many :personal_interests has_many :people, :through => :personal_interests end class Person < ActiveRecord::Base has_many :personal_interests has_many :activitie...

Polymorphic Association with multiple associations on the same model

I'm slightly confused about a polymorphic association I've got. I need an Article model to have a header image, and many images, but I want to have a single Image model. To make matters even more confusing, the Image model is polymorphic (to allow other resources to have many images). I'm using this association in my Article model: cla...

DRYing my has_many's in Rails

I have a model class that has, among other things: class Group < ActiveRecord::Base has_many :subscriptions has_many :users, :through => :subscriptions has_many :admins, :through => :subscriptions, :source => :user, :conditions => "subscriptions.role = #{ROLES[:admin]}" has_many :subscribers, :through => :subscriptions, :source...

Dynamic find conditions in active record

I have an index action in rails that can handle quite a few params eg: params[:first_name] # can be nil or first_name params[:age] # can be nil or age params[:country] # can be nil or country When finding users I would like to AND all the conditions that are not nil. This gives me 8 permutations of the find conditions. H...

Keeping the history of model associations

I have multiple models that need to have their history kept pretty much indefinitely or at least a very long time. The application I would keep track of daily attendance statistics for people in different organizations, and a couple of other similar associations. I've realized that I can't ever delete users due to the user not showing up...

ROR Associations group by query

I have the following 3 models and relationship between them: class Calendar < ActiveRecord::Base has_many:fiscalcalendars has_many:voucherdatas ,:through => :fiscalcalendars end class Fiscalcalendar < ActiveRecord::Base belongs_to :calendar has_many :voucherdatas end class Voucherdata < ActiveRecord::Base has_many :fisc...

Rails Trouble creating model instance with one to many relationship

I think there are a lot of places where my design may be screwing this up. I have very limited experience with Rails though. This is happening in Rails 2.3.2 with Postgres 8.3. We've got two tables in our DB. One called "survey" and one called "survey_timepoint". A survey can have multiple time points so in the survey_timepoint tabl...

Model-specific SQL logging in rails

In my rails application, I have a background process runner, model name Worker, that checks for new tasks to run every 10 seconds. This check generates two SQL queries each time - one to look for new jobs, one to delete old completed ones. The problem with this - the main log file gets spammed for each of those queries. Can I direct t...

Subsonic 3 and Activerecord isn't generating MySQL Stored Procedures

It seems that the included T4 templates (or the one in the SVN trunk for that matter) just skips generating SPs for MySQL... When running StoredProcedures.ttinclude together with MySQL.ttinclude, I get the error "Compiling transformation: The name 'GetSPs' does not exist in the current context". GetSPs is defined for SQLServer and I saw...

Bidirectional graph in rails

Hey guys, I have a simple model "Match" that is supposed to save the bi-directional link between two objects (of the same kind). class Match < ActiveRecord::Base belongs_to :obj1, :class_name => "MyModel", :foreign_key => :obj1_id belongs_to :obj2, :class_name => "MyModel", :foreign_key => :obj2_id ... end The problem I have is...

SQL Joins in Rails

Trying to work with an existing database in rails. The below works just great in MySQL Console: select title,blog_name from exp_weblog_titles JOIN exp_weblogs ON exp_weblog_titles.weblog_id = exp_weblogs.weblog_id LIMIT 1; +------------+---------------+ | title | blog_name | +------------+---------------+ | What We Do | chicag...

ActiveRecord Join table for legacy Database

I have a legacy database that I'm working on getting ActiveRecord to work with. I've run into a problem with join tables. I have the following: class TvShow < ActiveRecord::Base set_table_name "tvshow" set_primary_key "idShow" end class Episode < ActiveRecord::Base set_table_name "episode" set_primary_key "idEpisode" end ...

Activerecord error .include? nil

I am writing a rails app, which has some modules and methods loaded via the lib directory, we have also added some methods in restful authentication module, now everytime when i run using mongrel server on production i dont get this error, although because model classes and controller classes are cached, but in development i do get this ...

How to specify join conditions in Rails for belongs_to assocation?

Hi there, I'm trying to create an association between two models using join conditions rather than a foreign key. Does anyone know if this is possible? For example I want to join products to pricing points. A product has a price and a pricing point has a minimum and maximum amount and a name. Eg. Min = 0, Max = 20, Name = Less than $20...

Making a has_many/belongs_to association immutable

Let's say I have an invoice class that has many items related to it. How can I make sure that after the invoice is saved no items can be added or deleted from the invoice? I already use the immutable attributes plugin to handle regular fields but it doesn't handle associations. Using attr_readonly doesn't work either. ...