对多态关联进行排序的最佳方法

发布于 2024-10-27 20:40:20 字数 558 浏览 12 评论 0原文

因此 - 进行以下设置:

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

最后,我希望能够获取按客人全名订购的活动邀请。我不知道怎么办 - 谁能帮我解决这个问题?将不胜感激:)

So - having the following setup:

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

In the end, I want to be able to fetch an Events invitations ordered by the Guests full_name. I can't figure out how - anyone who can help me solve this? Would be greatly appreciated :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

对你的占有欲 2024-11-03 20:40:20

至于 full_name 是一个虚拟属性,我只能想象 ruby​​ 解决方案,而不是 sql

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

As far as full_name is a virtual attributes I can imagine only ruby solution, not sql

event = Event.find some event
event.invitations.sort_by(&:full_name) 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文