finder-sql

How do I pass a string to a has_many :finder_sql parameter?

In my application, a user has_many tickets. Unfortunately, the tickets table does not have a user_id: it has a user_login (it is a legacy database). I am going to change that someday, but for now this change would have too many implications. So how can I build a "user has_many :tickets" association through the login column? I tried t...

Rails find_by_sql - how to run generic queries

I need to execute this query to find out the next auto_increment value that will be used by MySQL for a particular table. find_by_sql ["select auto_increment from information_schema.tables where table_schema = ? and table_name = ? and auto_increment is not null", db_name, tbl_name] How to invoke this particular query? This works o...

setting attributes in Rails has_many :finder_sql relationship

My question is somewhat complicated, but bear with me. My project has a number of models: User, Program, and Team. Users belong to multiple Programs. Programs have multiple teams, but Teams belong to one program. For each Program a User belongs to, he can belong to multiple Teams. (Think of this in an athletic context where users ar...

Rails3: how to set default conditions to has_many

I have boxes and balls. Balls are in boxes. Ball can be either red and green. class Box < ActiveRecord::Base has_many :balls end class Ball < ActiveRecord::Base belongs_to :box scope :green, where(:color => "green") end I want to set has_many only with green balls. I know that finder_sql method exists, but i don't know how to s...

Effective usage of :finder_sql in has_many associations in Rails

In ActiveRecord models you can specify custom SQL-request for has_many associations. For example, class User < AciveRecord::Base has_many :events, :finder_sql => 'SELECT something complex' end While user.events returns what I need, the number of records returned can be huge, so I need to have a way to pass parameters for LIMIT. Is t...