问题描述
DHH撰写了文章,主张使用关注点.这似乎是一个好习惯,在很多情况下,它们可以与我的应用很好地配合使用.但是,在几种情况下,多个模型具有相似但略有不同的方法,例如:
DHH wrote an article advocating for the use of concerns. It seems like a good practice, and in a lot of cases, they work well with my app. There are several cases, however, where multiple models have similar but slightly different methods, such as:
def find_or_create_membership
user_membership = User::Membership.where(:group_id => self.group_id,
:user_id => self.invitee_id).first_or_create(:status => "invited")
end
和:
def find_or_create_membership
user_membership = User::Membership.where(:group_id => self.group_id,
:user_id => self.invitee_id).first_or_create(:status => "declined")
end
除了第一个将status
设置为"invited",第二个将"clined"设置为"declined"之外,这些方法是相同的.有什么办法可以通过关注将参数传递给这些方法吗?
These methods are identical save that the first sets status
to "invited" and the second to "declined". Is there a way I could pass an argument to these methods via a concern?
推荐答案
您可能对 Paramix 感兴趣.
不过,我自己从未使用过它.邓诺,闻起来像是假的好主意.
Never used it myself, though. Dunno, smells like a False-Good-Idea©.
这篇关于Rails:传递关注点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!