对多态关联进行排序的最佳方法
因此 - 进行以下设置:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
至于
full_name
是一个虚拟属性,我只能想象 ruby 解决方案,而不是 sqlAs far as
full_name
is a virtual attributes I can imagine only ruby solution, not sql