Arien Malec
2005-Mar-28 06:41 UTC
Active Record: finding records based on :belongs_to values
All --
I''ve got a bunch of different user types, all of whom have an Account.
Account holds first and last name, and a few other values; values for
the the other types vary considerably.
The problem I''m facing is that I want to order results by last, first
for all the user types, and I don''t see any way to do that except for
diving into SQL.
I don''t want to use single table inheritance, because of the wide
variation of colums for the various types, and because the don''t
naturally form a is-a relationships.
It looks, on browsing through the methods, as if my choices are:
1) Violate DRY, and duplicate account info over the user types (and
then I have to dup the account logic as well, so that''s out)
2) Get very wide tables and use single table inheritance, or
3) Override find_all?
If I override find_all, it looks like I want to set a join clause,
then delegate up to my superclass'' find_all? Does that make sense?
So I want to do something like:
def self.find_all(conditions = nil, orderings = nil, limit = nil, joins = nil)
acc_join = ''LEFT JOIN accounts ON myusertype.account_id = accounts.id
''
if joins
joins << acc_join
else
joins = acc_join
end
super
end
Am I on the right track? Is there some Rails magic that does this for me?
Thanks,
Arien
Seemingly Similar Threads
- belongs_to causing NoMethodError exceptions ... ?
- belongs_to causing endless loop on first call to save!
- When adding a record in console, a parameter comes in as null even when I set it
- am i the only one with this problem?
- Curious why this doesn't work. (has_many, belongs_to)
