11

我需要覆盖关系的名称,这是我的模型:

class User < ActiveRecord::Base

  has_many :class_rooms_member_ships

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

end

现在,当我想获得时,我需要使用另一个名称class_rooms :through=> :class_rooms_member_ships

我怎样才能做到这一点:

user.class_rooms
user.class_rooms_through

任何的想法 ?

4

2 回答 2

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

这应该有效。

于 2012-05-31T10:19:42.063 回答
0

像这样的东西应该工作:

has_many :classrooms_though_memberships, :through=> :class_rooms_member_ships, 
                                         :class_name => 'ClassRoom', 
                                         :foreign_key => 'class_room_id'
于 2012-05-31T05:11:11.510 回答