排序多态关联的最佳方法

时间:2011-03-26 20:59:46

标签: ruby-on-rails sorting polymorphic-associations nested-attributes

所以 - 进行以下设置:

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 订购的活动邀请。我无法弄清楚 - 有谁可以帮助我解决这个问题?非常感谢:)

1 个答案:

答案 0 :(得分:2)

至于full_name是虚拟属性我只能想象红宝石解决方案,而不是sql

event = Event.find some event
event.invitations.sort_by(&:full_name)