많은 초청자가 있지만 초청자가 하나있는 사용자 인스턴스가 있습니다. 해당 사용자 및 초대받은 사람과 관련된 초대 자 인스턴스에 액세스하려고합니다.하나가 있고 그 자체가 많습니다.
즉 :
user.inviter #=> return another user instance.
user.invitees #=> return a collection on user instances
User.rb
class User < ActiveRecord::Base
has_one :inviter, class_name: Invitation, foreign_key: :invitee_id
has_many :invitees, class_name: Invitation, foreign_key: :inviter_id
end
Invitation.rb
class Invitation < ActiveRecord::Base
belongs_to :inviter, class_name: User, foreign_key: :inviter_id
belongs_to :invitee, class_name: User, foreign_key: :invitee_id
end
이주
class CreateInvitations < ActiveRecord::Migration
def change
create_table :invitations do |t|
t.references :inviter, references: :user, index: true
t.references :invitee, references: :user, index: true
t.foreign_key :users, column: :inviter_id
t.foreign_key :users, column: :invitee_id
t.timestamps
end
end
end
이것은 초보자가있는 사용자에게 user.inviter
을 호출하면 사용자가 아닌 초대장 인스턴스를 반환하기 때문에 절반의 방식으로 작동합니다. user.invitees
과 동일은 초대 인스턴스의 콜렉션을 리턴합니다.
어떻게 작동시키는 지 알고 계십니까?
'inviter'및 'invitee'모델에 대한 코드를 제공 할 수 있습니까? – davidhu2000
초청자 초대 대상자 모델이 없습니다. 'inviter'가 초청 모델 – rararake