Rails: Passing an Argument to a Concern

半世苍凉 提交于 2019-12-23 10:07:12

问题


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

and:

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

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?


回答1:


You might be interested in Paramix.

Never used it myself, though. Dunno, smells like a False-Good-Idea©.



来源:https://stackoverflow.com/questions/15529079/rails-passing-an-argument-to-a-concern

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!