所以 - 具有以下设置:
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
最后,我希望能够获取由客人全名订购的事件邀请。我不知道如何 - 谁能帮我解决这个问题?将不胜感激:)
最佳答案
至于full_name
是一个虚拟属性我能想象只有ruby解决方案,没有sql
event = Event.find some event
event.invitations.sort_by(&:full_name)
关于ruby-on-rails - 排序多态关联的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5445264/