小编use*_*583的帖子

Rails 3.1限制用户创建的对象

我想限制用户可以创建的模型对象的数量.我已经尝试过以下但它不起作用.我理解rails 3.1中发生了一些变化,现在还不确定如何实现.

class User < ActiveRecord::Base
  has_many :things, :limit => 5, :dependent => :destroy # This doesn't work
end

class Things <ActiveRecord::Base
  belongs_to :user
end
Run Code Online (Sandbox Code Playgroud)

ruby activerecord ruby-on-rails limit ruby-on-rails-3.1

8
推荐指数
1
解决办法
3902
查看次数

如何更新/重命名载波上传的文件?

我无法弄清楚如何在 rails 3.2.6 中更新/重命名使用 Carrierwave-mongoid 上传/管理的文件。我想重命名数据库和文件系统中的文件。

像这样的东西也许...

def rename( id , new_name )
  f = UploadedFile.find(id)

  if f.update_attributes({ f.file.original_filename: new_name })  # this is WRONG, what is right???
    new_path = File.join( File.dirname( f.file.current_path ) , new_name ))
    FileUtils.mv( f.file.current_path , new_path )
  end

  return f
end
Run Code Online (Sandbox Code Playgroud)

让我补充一下这是在它已经上传之后。

rename ruby-on-rails file update-attributes carrierwave

5
推荐指数
2
解决办法
7282
查看次数