How do I create a Validation Error Log - ruby-on-rails

I have a rails 3 application that involves a lot of importing of CSV files from a third party into various rails models. I have developed some scripts to perform most of the heavy lifting and today I added a lot of validation to the models to make sure valid data is going into the models/tables.
I'd also like to do some logging of validation errors so I can stay on top of all of the data processing and catch and correct validation errors quickly. To that end, I have taken the following steps:
I created a ValidationError model to store validation errors.
I initially create an instance using new.
Once the object is built, I use valid? method and an if conditional to determine if the object it valid. If the the object is valid, I simply save it. If the object isn't valid I create a validation error record.
I use the errors method to populate my ValidationsError instance.
Then I create the ValidationError.create(validation_hash[])
I'll display the validationError model in my ActiveAdmin dashboard and send out an email when validation entries are created.
Question(s): Is my proposed approach to capturing validation error reasonable. Are there approaches that have been used by others that may be better/preferable to what I propose. Is anyone aware of any gems or built-in functionality that would accomplish what I'm trying to do.

Have a look at validation_rage gem. you might be able to use that gem to achieve what you want.

have also a look at this :)
Importing CSV file into multiple models at one time
and rail_admin_import gem, maybe you can expand with above staging table approach.

Related

Ruby on Rails Active Storage validation

Is it possible to easily disable model validation only when attaching files? We have lots of data from our previous system that won't pass new validation rules and we can't attach any files to those. I know we could make another set of validation rules based on creation date (to allow older not not so good data) but we have dozens of different models so it wouldn't make sense to duplicate everything and up keeping the code would become very messy. And when normally updating some old data we still would want to use the new validation rules anyway.
Normally I can skip validations using .save(validate: false) but that approach doesn't seem to work when attaching files. We are using Ruby on Rails 6.

Data model and storage for Rails app

At the moment ’m building a web app using Ruby on Rails. I try to get my head around the data model and database part. To make it easy to understand I’ll use IFTTT as an analogy:
In the app, the user can add different recipes, for example:
Facebook — Twitter
Weather — Send email
You name it...
Of course every recipe has its own fields and options. So the user can choose multiple recipes from the library and set options to every recipe. The library of recipes is defined in code.
I have a few questions regarding this setup and would be happy if I could get some directions:
Is it smart to serialize the options of a recipe into a single database field? Every recipe has different fields and I don‘t want a database table for every recipe type.
Or is it better to create a ‘key-value’ table with all the options of all the recipes?
How to handle validation? Can Virtus come in handy?
Is a NoSQL database a good fit for these kinds of applications?
Are there best practices for these kinds of applications/data models? Any help is welcome.
Thanks!
Rens
Not sure if SO is the best place for really general questions like this but I'll take a swing
1 && 2) Personally I'd give the recipe table an action_taken field, probably as a string, and fields for all the available, resulting actions as booleans. Then the only thing you really need to be careful of is making sure the action_taken field remains uniform
3) ActiveRecord has a pretty fleshed out validation suite built in. You can validate based on presence, uniqueness, inclusion in a set of elements, etc. You can also extra validations on the database if you feel like being extra safe
4) I would use PostgreSQL, seems to be the community standard so probably the easiest to get support with if you need it
Hope this helps

Restrictions on user-submitted code

I have a Ruby on Rails application, and one of its functions is to present JSON data to the user in table form. Before this step, I intend to add a way for users to tweak the JSON data by means of uploading their own Ruby code files that handle this.
This has its dangers. I definitely don't want any form of access (reading or writing) to the databases, nor do I want it to be able to call anything in another file. How can I limit the file in this way?
Essentially all I need is for the main code to call the function in the user-submitted file with the JSON as the parameter, and returning JSON back. All logic during this manipulation of the JSON must happen in and only in the user file.
I've looked around for ways to do this with no luck. I've seen this question:
Restricting access to user submitted code in Rails
The issue here is that I'd prefer an approach that doesn't require a gem. Also sandboxing seems rather complicated for the approach I want, which is a blanket restriction, and not specific things.
I intend to raise the $SAFE level to 4 before calling the user-supplied code/method. That doesn't seem to prevent calling other methods in the application though.

Spreadsheet-like way to add records to ActiveAdmin

I am looking for a way to add a lot of records at ones in ActiveAdmin. To be more specific, I have 2 models: Stores and Programs. Stores have many Programs.
I don't mind adding a Store using standard ActiveAdmin create view. But I would like a faster way to add programs in a spreadsheet-like way. I looked into best_in_place (https://github.com/bernat/best_in_place) but it doesn't do do adding records, just editing them.
Any suggestions? I would really appreciate it.
The short answer is Rails has nothing to help you do this. Rails has a defined convention for editing multiple objects if they belong to another object that can accept nested attributes for a few reasons, the most important of which is that there's a place to aggregate validation, as well as a standard way to differentiate each set of fields (the id). Neither of these are true during creation.
You can, however, manually work around this a couple of ways.
would be to simply write out the forms yourself, and put the logic to loop through them in your controller. This is going to be fragile, and you'll have issues getting validation to work properly, however.
would be to either create a new class that handles this single case, or try and adapt your existing Store class to handle nested attributes. There's a very solid explanation of how to do this here.

Paperclip and failed validation - avoid reupload

I am currently setting up Paperclip for a model with Rails 3. When one of the fields fails validation (title), the user has to upload the file again. This is not very user friendly :/
The recommendation from the Paperclip forum is to move the Paperclip stuff into a related model. My model is very simple with just a few fields, so I would like to avoid having two pages/steps for creating a record.
arts/create (when valid) -> arts_image/create
Any suggestions?
I use the two-step solution with a separate model. Although it's possible to code and hack your way around the default behaviour, you could also validate on the client-side with JS.
Look at this article http://ryantownsend.co.uk/articles/storing-paperclip-file-uploads-when-validation-fails.html
Cached version of the article: http://web.archive.org/web/20100919151143/http://ryantownsend.co.uk/articles/storing-paperclip-file-uploads-when-validation-fails.html
I have taken a different approach by 'serving' the file back to the client and re-accepting it when the form gets resubmitted.
https://stackoverflow.com/a/25853569/7693

Resources