I'm looking for a way to generate reports from view, like suppose I have model named "User" having fields "first_name, last_name,email, user_name ".
I will be having a view in which I will be having 2 select field, first one having model names and second one will be multiple select box where I can select model attribute that I can show in report.
How to generate report for selected attribute of specific model?
Personally I'd hardcode both select tags as not every field on the model might be worth filtering for. But for the sake of your question, ActiveRecord allows you to ask for the columns a model has in the database, e.g.
User.column_names
or
User.columns
Related
I'm busy with a rails 4/postgres project that is structured in such a way that requires dozens of fields for a specific object, this being company. Each company would include fields for eg name, location, header_bg, logo, contact name, contact cell etc so the fields very well could grow in the future. What is the best way to structure this DB.
The way I see it I have 3 options:
1) company table with all these fields in the same table
pros: simple, data all in one place and easy to query
cons: table could get very messy and requires manually editing table structure every time a new field is required
~~~~~~
2) company table with an additional company_options table a foreign key company_id. This table will also hold multiple fields
pros and cons are same as above except this will be more neater structure. Crucial data such as company name will go in company table, other data like location, theme, header_bg etc would go in company_options
~~~~~~
3) company table and a company_meta table. This will follow the Wordpress structure of the post_meta table. So it would have the fields: company_id, meta_key, meta_value. each field would go in its own row
pros: flexible, able to dynamically add options/fields without editing table structure
cons: not a simplistic approach with same data in same columns. would need to custom build this functionality for inserting, updating, validation displaying of data. Unless Rails has such a gem which I can't find?
~~~~~~
Any suggestions or additional options would be appreciated. Are there any rules of thumb for determining the kind of structure needed? I started out with the 1st option out of a matter of simplicity but then started to build the 3rd and the amount of code just didn't seem right for what seems like such a common need.
Thanks!
Plan your database schema for the future:
Will a company be able to have several phones?
For repeating information, use a new table:
rails g model phone number:string type:string
rails g migration add_company_id_to_phones company:references
This way you can add several phones and navigate your database to get each phone.
Will your field only have one of a kind?
For example, you might only need one name field for each company. If this is the case, add it directly to the companies table:
rails g migration add_fields_to_companies name:string
Is your data completely optional? Is it unstructured?
Add a JSONB field to your table, assuming you are using Postgresql:
rails g migration add_details_to_companies details:jsonb
With JSONB data type you can use unstructured data like this:
Company.create({
name: "AwesomeCompanyCorp",
details: {
"main phone": "000-0000-0000",
"secondary phone": "000-0000-0000",
"support email": "support email",
"location": "Main Avenue 165",
"latitude": "22.330213123",
"longitude": "60.000012312"
}
})
With JSONB you are able to have unstructured data, but be able to query the field like this:
Company.where("details->>'main phone' = ?", "000-0000-0000")
With this on mind, choose the best approach that fits your case.
(I've made up the example below as it's clearer than going through my specific case.)
Suppose I have a form for a User, and that the underlying User table has a personality_traits field; an array of the user's personality traits. Is there a standard way in rails to create a drop-down list so that the user can pick any number of a selection of attributes (e.g., "easy-going", "fun-loving", "quick-tempered"), and have these ultimately feed into the personality_traits field as an array? If not, does anyone know of a particularly good method?
bootstrap multiselect will do what you're looking for
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.
As a paranoid entry-level developer I was wondering if it is possible for a user to use some kind of query to insert data into a database through a form with a select field.
More specifically lets say in my db I have a Gender column (data type text) and in my form I am using the select tag and passing in 2 options Male and Female. Although in the html the user only has 2 options to select from but the db doesn't know that. The gender column will pretty much accept anything. I just wanted to know if a nuisance user can disregard the select options and somehow insert a silly answer into the Gender column? If so, how can I protect from that.
The thing you are talking about here is the validations need to be done before entering data to the database. Invalid data can come from anywhere ( directly from controller or from GUI) and its the responsibility of the model to validate the data before it commits to the database.
Go through these links.
http://api.rubyonrails.org/classes/ActiveRecord/Validations.html
http://guides.rubyonrails.org/active_record_validations_callbacks.html
I'm currently building my first Rails 3 app, but can't quite figure out how to get one piece of functionality.
I have a temperature field, but rather than using <%= f.text_field :temp %>, I'd like to have two dropdowns. The first dropdown would allow choosing 97-99, while the second dropdown would allow choosing 0-9. After the user has made their selection and saved, the results should be concatenated into e.g. 98.2
What is the best way to achieve this in Rails 3? Thanks!
I suggest creating two virtual attributes, one for each part of the temperature (97-99 and 0-9). Then define a before_validation method on the model that takes those two values (that aren't persisted to the database) and sets the temp attribute accordingly (that is persisted to the database). This way you can validate the temp value as you normally would and query it as you would any other regular value in the database. It also keeps your view code easy on the eyes. There are other ways to accomplish this though.
Ryan Bates has a great screencast on virtual attributes.
http://railscasts.com/episodes/167-more-on-virtual-attributes
You could make two attributes in whatever model you use. One for each field you want to use. Then when you hit save you have code in the model and uses these attributes to form the temperature attribute.