0

我的用户模型如下图user.rb

has_many :authorised_downloads, 
:class_name=>"Downloads",
:primary_key => 'id', 
:foreign_key=> :authorised_user_id

下载模型download.rb

belongs_to :authorised_users, 
:class_name => "User",
:primary_key => 'id',
:foreign_key=> :authorised_user_id

下载控制器

def download
@download = Download.find(params[:id])
if @download.authorised_users.includes?(current_user)
  send_file(@download.upload_file.path,
  :filename => @download.name,       
  :x_sendfile=>true,
  :url_based_filename => true )               
  flash[:notice] = "Your file has been downloaded"
else
   flash[:notice] = "You must buy the product first!"
end
end

在我的下载视图中,当我单击下载链接时,if @download.authorised_users.includes?(current_user)下载控制器中的行突出显示并显示以下错误

undefined method `includes?' for #<User:0x00000005c2ff10>

我不明白我哪里错了。您的帮助将不胜感激

4

1 回答 1

0

@download.authorised_users返回用户的实例,而不是集合。公元前belongs_to :authorised_users

尝试:

belongs_to :authorised_user, :class_name => "User"

在控制器中:

if @download.authorised_user == current_user
于 2013-09-18T07:57:28.440 回答