views:

121

answers:

1

I have 3 models

sites, user_favorites and users. Relevant relationships:

class Site < ActiveRecord::Base
  has_many :users, :through => :user_favorites

class UserFavorite < ActiveRecord::Base
  belongs_to :user, :counter_cache => true
  belongs_to :site
end

class User < ActiveRecord:Base
  has_many :user_favorites
  has_many :sites, :through => :user_favorites

All of that works just fine. I'd like to add a new attribute to the Site model to indicate which user created it.

I don't believe this constitutes a has_and_belongs_to_many scenario. A site has many users through user_favorites but I want it to belong to a single user reflecting the owner/creator.

I'm wondering what the ORM best practice is for this. SQL wise I'd just use different joins depending on what I was trying to query with a created_by FK in Site. Sorry if I'm missing something basic here.

Thanks

A: 
class Site < ActiveRecord::Base
  belongs_to :creator, :class_name => 'User'
  # ...
end

should be sufficient. Right? You can have multiple relationships between the same models, but you will find it extremely beneficial for organizational purposes to explicitly name them when such is the case.

Matchu
works perfectly, makes perfect sense. Appreciate the help still getting my head around ORM.
Nick