my user model is as shown below user.rb
has_many :authorised_downloads,
:class_name=>"Downloads",
:primary_key => 'id',
:foreign_key=> :authorised_user_id
downloads model download.rb
belongs_to :authorised_users,
:class_name => "User",
:primary_key => 'id',
:foreign_key=> :authorised_user_id
downloads controller
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
in my downloads view, when i click the download link, the line if @download.authorised_users.includes?(current_user)
in the download controller is highlighted with the following error
undefined method `includes?' for #<User:0x00000005c2ff10>
I dont understand where i when wrong. Your assistance will be highly appreciated
@download.authorised_users
return an instance of User, not collection. B/c belongs_to :authorised_users
Try:
belongs_to :authorised_user, :class_name => "User"
And in controller:
if @download.authorised_user == current_user