Errors on non model fields in rails - ruby-on-rails

What's the best way to report errors on form fields not associated with a particular model in Rails? As an example, I have a form for the batch creation of user accounts with random users / passwords. It takes as inputs the quantity of users to make, information about what attributes all users should have, and information about the batch which is stored in a user_batches model associated with the created users.
Ideally there would be some errors_on like way to list errors coming from the quantity field, which is associated with no model, the user information fields, associated with the user records that get created, and the user_batches model with minimal code.
This also applies to search forms and the like, which don't get run through AR validations. Any ideas?

You can add your own errors manually to your model object like this.
#user_batch.errors.add_to_base("Foo")

Related

Using Ruby on Rails, what is an efficient method of ordering separate users post_id's sequentially?

My domain structure is similar to this:
www.domain.com/:user_id/post/:post_id
i.e. www.domain.com/user1/post/1
I would like it to work where each user's post increments up the user's last :post_id, disregarding the actual ID of the database row, as multiple users posts will be stored there, like so:
www.domain.com/user1/post/1
www.domain.com/user1/post/2
www.domain.com/user2/post/1
www.domain.com/user2/post/2
I understand I will need a "secondary_id" column in the Post database and before committing a post to the database I will need to query the last post of the user to obtain that secondary_id to increment but I'm unsure where this logic would best reside in my app structure and what can I do to simplify/automate the process?
Also, how can I avoid race conditions on the secondary_id if this where implemented in a team environment where users could be submitting posts in between when another user has queried for the last secondary_id to increment and the second user would error out since the other user got to that secondary_id first?

Retrieving field params field in rails

I have been trying to analyze this problem very closely but I am still yet to find a good way to approach it. (Hope my explanation is good enough)
So I have three models [user, status and milestone]
the status model belongs to the user model
The milestone model belongs to status model and also to the user model through the status model
Okay so I want to tie each milestone to a model by doing something like (milestone.build_status, this is pretty easy from the CLI, I have tested and tried it, and it works as expected.)
So the big issue I am having is on the web page. I am displaying all the statuses (I have already handled cases when the user enter a status) to user with a corresponding text field where they can enter their milestone, well when I do I post I can only get the params of the text field that was supplied (duh! isnt that obvious).
My question would be what are some possible approaches that I can use to figure which particular status that the user entered the milestone for.
I think you may be looking for Active Record Nested Attributes
This will allow your forms for your object of the User model to also accept input for its associated Status and/or Milestone objects for creating and updating each associated records all in one transaction.

Preventing duplicate model objects with nested forms in rails 3

I have a form set up for a Recipes model that accepts_nested_attributes_for Ingredients. The form and saving works fine, however, I want to check for the existence of each Ingredient in the database. The form seems to be creating new records for ingredients, even if they already exist in the database.
Recipes and Ingredients are related using a 'has_many :through' relationship. Is there a clean way to make sure that ingredients are duplicated when the form is submitted, but the relationships are still established?
similar question might help you on this, although the solution is not that eligant
Rails nested form on many-to-many: how to prevent duplicates?
It is about how you are taking input from the user - your forms are.
If you have a multiple select input for ingredients in new/edit form of recipe and you show all the existing ingredients there, user can choose the existing ingredients and the POST/PUT from these forms will have the existing ingredients' ids as part of form data. This will not create new ingredients then.

A few intermediate Rails3 Questions

I'm building an app called "CourseWork to dig into rails/develop my skills and I have a question about how to structure it. Users have a resource called "CourseGrading" that is able to create categories and belongs to "Course". Each "category" should have a name, a percentage out of 100 and a course_id. I need to add these percentages together and alert users if the total isn't 100 while still saving.
Then the user's generated "categories" should populate an enum_string specific to that user in a resource called "CourseAssignment" which has a name, description, category and finalgrade.
Can anyone give hints or resources for how best to accomplish this? Thanks
You probably want to take a look at Active Record Callbacks. These will allow you to insert some code to be run when creating/validating/updating/deleting models.
You should probably make use of the ActiveRecord validations.
Check out this guide that explains how to write your own custom validator. Your custom validator would run when the form gets submitted, and in it, you would grab the percentage params and do your check. If it's not what you expect, you can just add an error to the form and the validation process will just kick the user back to the form page and display the error.

Multiple non-related models in one Rails form

I am building a blog-style application in Rails 3 where multiple users are able to post some news. After the login (which is realized with "Authlogic") the user values are stored in a own model called e.g. "UserSession". The form for the post contains title, content etc. and the username should be stored with a hidden form.
I think that the two models don't need to be related to each other (by that I mean a :has_many - :belongs_to relationship) because there isn't any further usage for that information.
Do I really not need this relation? And how could I realize the form?
For Authlogic is it important to remember that the 'UserSession' does not correspond to any database tables (i.e. you would never use a has_many or has_one 'UserSession'). I think the relationship you are looking for is:
User has many Posts
Blog belongs to User
The reason? It is always a good idea to associate a record with the 'owner' so that the owner can later modify or delete the record. I hope this helps.

Resources