简体   繁体   中英

What command can I use to create this Ruby on Rails model?

I'm creating a model in Ruby on Rails that will act like a file system. You'll have assets (like files) that could either be folders or files themselves. How can I create a command for this?

Asset
  id (unique auto-incrementing number)
  name
  is_directory (bool)
  user_id (id of the owner)
  parent_asset_id (id of parent directory, or null if under the root)
  access_token (randomly generated token, used to send shareable links)
  contents

I'm thinking something like:

rails generate model Asset 
  name:string 
  is_directory:boolean 
  user_id:integer 
  parent_asset_id:integer 
  access_token:string contents:??

Some questions I have:

  1. What's the difference betweeen blob vs longblob vs mediumblob vs longtext vs etc, and which would I want to use? (The assets are essentially text... not sure what the max size will be yet)

  2. Is the parent_asset_id a good naming convention, or is there something else that would make Rails give me some secret sauce, similar to why I picked the name user_id (to match the User model)?

  3. Is there a way to declare a default random string value for the access_token? (The access token will be used for a shareable link to the asset)

  4. Anything else I'm overlooking?

This is a detailed question, so I hope it serves as a case study for anyone looking to implement something like a file system in RoR.

Obviously if you really wanted to implement a file system you'd use an actual file system or Amazon S3... but if you want a light-weight file-like system in RoR, this seems like the best approach.

First off, I strongly recommend you consider an existing gem like Paperclip , which will handle a lot of these details for you.

Answers in order:

  1. You would use the binary field type to store general data, but if you use Paperclip there are some specific fields you would need to use instead, which are explained in Paperclip's docs.

  2. If by parent_asset_id you really mean the asset can 'belong' to many other models, then look into setting up a polymorphic relationship, with an id and type field. If instead you mean storing the path to the stored file, then Paperclip handles this for you. See #3 for details...

  3. You can access a stored file on Paperclip by calling something as simple as asset.url in your view. If you wish to go manual and insert a random code, you can insert a callback into your Asset.rb model that does something like:

     before_create:generate_key def generate_key self.key = ActiveSupport::SecureRandom.hex end

S3 is not a complex system to set up on Rails, and it is far more flexible and scaleable than storing the files elsewhere - however, if you want to, then use the 'assets' path.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM