Tag: has many polymorphs

Ruby on Rails 3:结合多个has_many或has_many_through关联的结果

我有以下型号。 用户有UserActions,一个可能的UserAction可以是ContactAction(UserAction是一个多态)。 还有LoginAction等其他动作。所以 class User “ContactAction” has_many:user_actions has_many_polymorphs:user_actionables,:from => [:contact_actions,…],:through =>:user_actions 结束 class UserAction true 结束 class ContactAction <AR :: Base belongs_to:用户 named_scope:pending,… named_scope:active,… 结束 这个想法是一个ContactAction加入两个用户(在app中有其他后果),并且总是有一个接收端和一个发送端。 同时,ContactAction可以具有不同的状态,例如过期,待定等。 我可以说@user.contact_actions.pending或@user.contact_requests.expired列出用户发送或接收的所有待处理/过期请求。 这很好用。 我现在想要的是一种加入两种类型的ContactAction的方法。 即@user.contact_actions_or_requests 。 我尝试了以下方法: 类用户 def contact_actions_or_requests self.contact_actions + self.contact_requests 结束 # 要么 has_many:contact_actions_or_requests,:finder_sql => …,:counter_sql => … 结束 但是所有这些都存在这样的问题:在关联之上不可能使用额外的finder或named_scope,例如@user.contact_actions_or_requests.find(…)或@user.contact_actions_or_requests.expired 。 基本上,我需要一种表达1:n关联的方法,它有两条不同的路径。 一个是User -> ContactAction.user_id ,另一个是User […]