【发布时间】:2015-12-16 16:43:21
【问题描述】:
我在两个不同的控制器(帖子和板)中有两种方法。它们几乎相同。区别只是模型-实例-关联名称。要干这个我想在模块中编写方法,但是如何在 Post 和 Board 之间共享它?
def init_post_comments
@user = current_user
a = @user.posts.pluck(:id) # not very nice...
b=params[:post_ids] ||= []
b = b.map(&:to_i)
follow = b - a
unfollow = a - b
follow.each do |id| # checkbox just checked
@post = Post.find_by_id(id)
if @post.users.empty?
@post.update_attribute(:new_follow, true)
end
@user.posts << @post
end
unfollow.each do |id| # if checkbox was unchecked
@post = Post.find_by_id(id)
remove_post_from_user(@post)# here we destroy association
end
if follow.size > 0
get_post_comments_data
end
redirect_to :back
end
更新好的,如果我将方法转移到模型的关注点,我应该如何在这里处理关联?在这里@user.posts.pluck(:id) 和这里@user.boards.pluck(:id) 用我可以替换帖子和板,以便它可以与它们一起使用?
【问题讨论】:
-
你能把你的问题说得更具体一些吗?什么是董事会?具体来说,您想要完成什么?
-
您可以在这两个类中包含该模块,然后您可以在任一类的实例上调用它。
-
@brito 如果在我的代码中您将 Post-@post-posts 替换为 Board-@board-boards,这将是相同的方法,但适用于另一个模型。现在我在两个不同的控制器 PostsController 和 BoardsController 中编写了两次代码。但没关系,它可以是 Foo 和 Bar。
-
@MaxWilliams 好的,但是这个方法对于共享模块应该是什么样子的?这是一个问题。
-
这大部分是业务逻辑,应该放在模型中,如果是通过模块共享的方法,如果你想要类,可以参考
self。
标签: ruby-on-rails associations relationship activesupport-concern