所以 - 进行以下设置:
class Event < ActiveRecord::Base
has_many :invitations
end
class Invitation < ActiveRecord::Base
belongs_to :guest, :polymorphic => true
belongs_to :event
end
class Member > ActiveRecord::Base
has_many :invitations, :as => :guest
def full_name
[first_name, last_name].join(" ")
end
end
class Visitor > ActiveRecord::Base
has_many :invitations, :as => :guest
def full_name
name
end
end
最后,我希望能够获取由Guests full_name 订购的活动邀请。我无法弄清楚 - 有谁可以帮助我解决这个问题?非常感谢:)
答案 0 :(得分:2)
至于full_name
是虚拟属性我只能想象红宝石解决方案,而不是sql
event = Event.find some event
event.invitations.sort_by(&:full_name)