Form for one model with many objects - ruby-on-rails

UPD.
I need to display multiple records of a single model in a form.Then after updating some values I need to save all the records data at once..that is my form view should work like a spread sheet.
The requirement is, from one table I need to get data and populate all rows in a form. After that I have to edit some columns then and I should save all records of this model to database at a time.

Here is Railscast which presents how to edit multiple records in one form.

Related

Delphi one unique form for insert and update database record

In my app does I need to have separate forms for:
Form for showing data i grid
Form for insert
Form for update
Or I just need two: one for displaying data, and one unique for insert and update.
In unique insert form just pass selected id from row and with query load it in fields. First in insert form will declare boolean variable like editMode false. If passed id exist in db activate query and fill fields with values.
Is this good or bad practice? How big companies or skilled developers separate this?
I don't need someone to write code for me. I just want to know proper algorithm for this. Thanks

Multiple DropDownLists and a View and View model

A design question on multiple DropDownLists on a View.
What is the best approach to displaying multiple DropDownLists on a View? Is it possible to pass multiple DropDownLists to a View model?
For example:
If I had the following View models: Student, Course, and Enrollment. And on the Index View page, I'm returning and displaying all students. But I'd like to filter the number of Students being returned based on values selected from multiple DropDownLists, such as Suburb, LanguageSpoken...
Each DropDownList will be populated from the database.
Two approaches that I can think:
In the Student View model, have a (fully populated) collection of Suburb, LanguageSpoken... .
Have multiple Models passed to the View model. In this case one Model will be a Student, and the other Models will be each DropDownLists. I haven't looked into the details of this yet.
I don't like the first approach, as it sounds very inefficient, i.e. each Student hold collections of list of all Suburbs, Languages... Also, not sure if the second approach is possible or even a good idea.
I'm using ASP.NET MVC 5 and Entity Framework 6.
Ok, after alot of reading up and trying a number of things, I've finally have a (simple) solution: Drop Down Lists using ViewBag.
E.g. the Student controller returns a Student viewmodel to the View, and any further models such as postcodes, languages... are returned via ViewBag to the View.
I figure ViewBag is appropriately used for such data, i.e. lookup (static) type data.

Splitting rails models vs. using one model for a combined form

I am working on an application that allows various people from different departments in a company log in and edit a form. Each department has specific fields in the form that only they are allowed to edit and view.
Would it be more organized code if I create one model for the entire set of fields and based on who logs in, render a different form containing the necessary fields? Or would it be better to split the department-specific fields into their own models and then based on who logs in, render a form containing their specific fields and relate the records from different departments together through a foreign key of some sort?
"It depends" is probably the best answer.
How many fields do you have? How much logic exists per department form type?
If you split all the fields to separate models (and corresponding tables) it will become a hassle if you ever want a full overview of the forms after they have been filled in, because they would be in separate tables. You'd have to go through every association of the form to get the full form.
If you still want to split them, because you have a lot of logic per department, you could point all your models to one 'main' table that contains all the fields, using table_name. That way you have all the data in one table and you can easily retrieve it. However, you'd also have to save the type of form in order to retrieve it in the correct model, probably more trouble than it's worth. (If it's the best thing to save all your fields in one table depends as well, how often does it get retrieved or does it get inserted/updated more often.)
Simple forms will probably fit just fine in one model. Unless you have a lot of non-sharable code per department it probably isn't worth it to split the form into separate models. If you do split the form into models you probably should create one main form model and have the department models extend it for whatever bit of reusable form logic there is.
After all that, I would say, I prefer simplicity so I'd probably save it in one model.
I'd split the fields and then you can setup your models with accepts_nested_attributes_for to build your form appropriately.

Add form fields to single model form in rails

I have a model called Application, and I need users to be able to add their classes and grades to a column in the database. Is there a way to do this without creating a separate model?
I need users able to press "Add Grades" as many times as they like, creating two new form fields to enter in the class name and grade they got.
I recommend creating a grades model - it is the Rails way. A Grade can belong to an Application. An Application can have many Grades. Each Grade can then have two attributes - a class and a grade.
Alternatively, you can store all your grades for a single application in a single column, but you'll be responsible for the format that they're saved in, ensuring that they are serialized at some point prior to being saved, parsing when you load each Application, etc. Finally, you'll be coming up with additional fields to deal with the parsed grades.
All of this can be avoided by simply creating the Grade model and setting up the appropriate relationship with the Application model.

DB grid : How to use a column of the current row of one as an index into another?

I am not sure if the question title is clear enough, please feel free to edit it.
Basically, I have two DB grids which reflect two database tables, each grid showing one.
When the user selects a row in the first table (let's call it oders), I want to update the second with details of any rows matching a column of the selected row of the first table.
Say, for instance that table orders has a column customer_id and I want to populate the second table (let's call it order_details) with details of all orders from that customer, one order per row.
I can connect up 2 # datasource, query and connection to the two TDbGrids, but I am stuck as to how to code order_details SQL.
The SQL for orders is just SELECT * from orders, but the other?
I want something like SELECT * from order_details WHERE cutomer_id=<orderQuery>.currentRow.FieldByName("customer_id").AsInteger - but I don't know how to do that ...
Can someone help me with some Delphi code?
Also, once I set up that relationship, will selecting a new row in the orders DB grid automatically update the order_details DB grid? Or do I need to add code for that.
P.s I know that there is no books tag anymore (more's the pity), but can someone recommend a good book which explains the fundamentals of programming DB aware controls? I obviously need one. Thanks
Use a parameterized query for the detail (child) database:
SELECT * FROM Order_Details od WHERE od.CustomerID = :CustomerID
Then set the child query's MasterSource to the parent (Order) datasource, and the MasterFields to CustomerID. (If there are multiple columns that link the two, separate them by ;, as in CustomerID;OrderNumber.)
Every time you scroll the parent (change the selected record in the parent DBGrid), the child query will be executed with the ID of the parent row passed as a parameter automatically.

Resources