Rails ActiveStorage use without upload interface? - ruby-on-rails

I wish to get the benefits of Rails Active Storage, eg the metadata of files being stored in my database etc, but the files are already on my S3 compatible storage.
Is there a way to add the appropriate database records and link to the right instance of my model?
I'm thinking along the lines of a rake task to iterate all model instances and find a matching file in the service somehow?

Related

How to write test for Ruby on Rails API that is built on an existing database?

I am trying to build a read-only Rails API that uses another Rails application's DB as its database.
So far I configured the read-only API's database.yml to point to the existing databases, generated models via rails g model but did not run the migration since I already have the corresponding tables in the existing databases.
I wanted to follow TDD approach and started to write tests but my problem is I cannot create instances of models since I did not create the table for that model.
Example Case
I generated a model named project which exists as a table in the existing DB that I am going to use to read data, but since I did not run the migration for that model it does not exist in the current API's DB.
So, I just dumped the schema.rb from the existing DB, copied it as my read-only API's db/schema.rb and run rake db:schema:load when RAILS_ENV=test.
I wonder if there's a better way to do this? Is my way of writing a read-only API like this is correct? I am open to any suggestions for this topic.
Cheers.

uploading multiple files on carrierwave (implementing arrays) while using sqlite

I'm building a web-app using ruby on rails (4.2.5),
I need the user to be able to upload multiple files as attachments when creating an object (called "Request" in this case).
I was able to implement carrierwave (0.9) and users can upload a file to each request, but turns out that the multiple files functionality requires an array-type column, which is not available as a datatype in the SQLite database which I am using.
From the above things, I think it seems that you can use JSON to do that, but I don't know how that works.
I would like to know how I can work around this problem and allow the user to upload multiple files to the request, even if I should use an entirely different method.
Thank you

Rails Application :: Where to put default texts for the models

In my Rails app I need to maintain too many default texts. For example, I have a Hotel model. When someone creates a hotel, some default email templates, default sms templates get created automatically for that hotel. Currently I am maintaining them inside a Constants modules and access them like:
Constants::DEFAULT_RESERVATION_EMAIL_TEMPLATE
Constants::DEFAULT_RESERVATION_SMS_TEMPLATE
etc.
I wonder if there any other convenient and efficient ways to maintain those default texts as it seems Constants module is going huge in each days. I am thinking like I can manage them in a yaml file so that it does not affect on memory and I can read from the yaml file when necessary.
I would use the normal i18n yml files for that. You can manage multiple languages through yml files. And you can easily change the content.
But you also can use a database for your backend.
Following links could give you an idea:
http://asciicasts.com/episodes/256-i18n-backends
Rails: store translations in database
http://franck.verrot.fr/blog/2010/02/27/rails-3-let-activerecord-manage-your-translations/
https://github.com/svenfuchs/globalize3 (if you also have default texts for model attributes, you can define them in a yml file and save them in the database after create, or something similar..)

What is the best way to model upload functionality for files that are "bulk" in nature?

I want users to be able to upload data files that will be processed in the background on my application.
I see many examples of using paperclip to allow files to be attached to specific models. But these files don't have a one-to-one correspondence to any of my models. How should I model this in Rails 3?
The way I would approach it, barring any input from people smarter than me, is to define "file types" for a specific model that is associated with the user account model itself. Then the upload process would place those files in a specific directory where they would be picked up by a poller that would then process the files.
Polymorphic Paperclip?
http://burm.net/2008/10/17/ruby-on-rails-polymorphic-paperclip-plugin-tutorial/

Add a file to a database in a Ruby on Rails application?

I've only just started learning ruby on rails and I would like to create an application that will allow me to add files to the database. Currently, I'm developing the rails application using the Aptana plugin for Eclipse and the application is using the default sqllite db.
I have tried generating a scaffold with the following parameters: documents title:string file:varbinary. Then I do a 'rake'->'db'->'migrate'. When I migrate to localhost/documents and click on 'New Document' the application fails and displays an error.
What I would like to do is click on 'New Document', have a field that will allow me to browse for a document on my local computer, select the document and add it to the db on the rails application.
Paperclip is more recommended than attachment_fu these days. It is really simple and easy to use with your active record model.
Is it a particular kind of file you want to add?
I just ask because if it's not data of a kind that benefits from being in a database ( textual data might be searchable, binary data is not ) then you are much better storing it in the filesystem and serving it up straight - especially for stuff like images or video - rather than inserting it into a database and having to go through your application every time a user requests it.
I'm not saying that there aren't any reasons you might want to have a file in the database, but I treat that as a last resort and in ten years of web programming I've not come across a case where it was necessary.
I would highly recommend the attachment_fu plugin as this lets you create models with attachments pretty nicely, Paperclip plugin is another good one also!
If you have trouble deciding which one to use, as far as i can remember, Paperclip makes it easier for multiple attachments, such as an Album has many Photos, and Attachment_fu is easier for single attachments such as a User has one display picture.
We do something like this on a site I'm managing. Instead of storing these files in a database, I'd agree with the other posters here and recommend you try something like Paperclip.
One caveat: if you want access control, make sure that paperclip doesn't save your files somewhere under /public, where anyone could possibly access them if they knew the URL. Deliver files to the user via send_file in your controller.

Resources