Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rails, how to alias a relation in model?

I need to override the name of a relation, here is my model:

class User < ActiveRecord::Base

  has_many :class_rooms_member_ships

  has_many :class_rooms
  has_many :class_rooms, :through=> :class_rooms_member_ships

end

now, I need another name to use when I want to get class_rooms :through=> :class_rooms_member_ships

how can I achieve this:

user.class_rooms
user.class_rooms_through

Any idea ?

like image 548
simo Avatar asked May 31 '12 04:05

simo


1 Answers

has_many :classrooms_though_memberships, :through=> :class_rooms_member_ships, 
                                     :class_name => 'ClassRoom', 
                                     :foreign_key => 'class_room_id',
                                     :source => :class_room

This should work.

like image 86
Seb Jacobs Avatar answered Nov 01 '22 23:11

Seb Jacobs