【发布时间】:2014-12-01 16:19:33
【问题描述】:
我正在使用 Rails evoting 应用程序。
我有以下模型:User、Election、Contestant。
我希望允许多个用户在特定选举中为参赛者投票,当用户在该选举中投票时,应隐藏指向每个用户的链接。
这个关联太复杂了,我弄不明白。
这些是我的联想:
class User < ActiveRecord::Base
has_many :contestants
has_many :contestant_votes
end
class Contestant < ActiveRecord::Base
mount_uploader :avatar, AvatarUploader
belongs_to :user
has_many :contestant_votes
has_many :elections
def votes
read_attribute(:votes) || contestant_votes.sum(:value)
end
end
class Election < ActiveRecord::Base
has_many :contestants
end
如果当前用户在选举中为某位选手投票,我如何隐藏投票链接?
【问题讨论】:
-
您可以在用户模型中添加一个“已投票”标志来指示用户是否已经投票。并且链接隐藏应该在控制器中完成。
-
好吧,如果有新的选举呢?我建议投票的文件仍然是错误的
标签: ruby ruby-on-rails-4 activerecord model-view-controller associations