(Rails newbie) I would like to offer users multiple checkboxes "Select All That Apply".
What is the usual way one handles that data? I was thinking I could just have a column that contained a series of selected strings.
Which foods do you like? Check all that apply
• Pizza
• Ice Cream
• Fried Chicken
• Tacos
If a user checked Pizza and Ice Cream and submitted, I was thinking I would have a column (SurveyQuestion1) that would look like "Pizza,Ice Cream".
I don't even know where to start here.
EDIT to be more specific:
the way I actually have it set up is I have a User model, a Product model and a Survey model. The form submits to all three models with nested attributes. The User enters info about themselves, the product they bought and a few surevy-ish/preference questions at the end. The User has_many Products and Surveys. The Products belongs_to User, as does Surveys. It's been working great for me until I've become hung up on this "select all that apply" type of question.
If you are using a SQL database, as it seems, you should take a look at many-to-many associations. Here's a RailsGuide about that: http://guides.rubyonrails.org/association_basics.html#the-has-many-through-association.
You need to create a model for your foods. Then when a user selects his foods you store his selections on an association table that is going to have an entry for each user selected food.
Related
I am building an e-commerce app the hard way(no spree, shoppe, etc) so that I can learn rails. Currently, I have a restaurant model, meals model, and orders model. Users(using devise) can open meals, fill in an order's form with an attribute :quantity and send the order to their carts(keeping :restaurant_id, meal_id, :qty). However, meals in restaurants normally have sizes and supplementals(with cheese, with ketchup, etc.). Ideally, I would build something like spree_flexi_variants
but I just can't see how to do this... Should I nest "characteristics" and "supplementals"(becoming 2 levels deep as meals is currently nested in restaurant) in the meals, or somehow add an attribute to the meals model? Thank you guys for any suggestions!
I'm assuming that your meal object has the supplements as child items, in which case the order is also going to need child items, in this case they would be a join between supplements and order.
You don't HAVE to do it this way, it's just one possible approach. You could store the selected supplements as a serialised hash on the order row instead.
If this is a project aimed at learning, I would try both and see which feels nicest - it's all useful practice!
I'm a bit confused on the topic of index columns in model associations.
(From the The Rails 4 Way) We have User, Timesheet, and Expense Report models.
The User model:
has_many: timesheets
has_many: expense_reports
(along with the corresponding belongs_to in the other models)
The Rails 4 Way book says to add_index into the timesheets and expense_reports model as so:
add_index :timesheets, :user_id
add_index :expense_reports, :user_id
I don't understand the reasoning for adding an index after every foreign key column. The timesheet and expense_report tables already have a primary_key column so why isn't that used for "performance boos?" Adding two additional indices seems redundant to me? Can someone explain the benefit?
I can see where your frustration comes from, just follow along and I am sure you'll get it pretty fast...
Problem:
When you create a column in a database, it is vital to consider whether you will need to find and retrieve records from that column. So, lets say we have a User table and every user has an email, that in many applications is used for authentication and authorization of users. When we allow users to log in to our application, we will need to find the user record corresponding to the submitted email address. Unfortunately, the only way to find a user by email address is to scan through each user row in the database and compare its email attribute to the given email - which, therefore, implies we might have to examine every row(since the user can be the last person inside the database). This would take a lot of time as you can imagine. Simply, this is not good.
Solution:
Putting an index on the email column would fix the problem. Think of it as an appendix at the end of the book. In a book, to find all the occurrences of a given string, say “foobar”, you would have to scan each page for “foobar” - the paper version of a full-table scan. With a book index, on the other hand, you can just look up “foobar” in the index to see all the pages containing “foobar”. A database index works essentially the same way.
I hope this helps you out.
Within my rails app I have two models. A User model and a Book model. A user uploads and has many books.
I'm trying to add 10 predefined categories to the user model, so that a user assigns as many of the 10 categories to themselves as they wish. For example, if i'm a user that uploads a several books, and the categories I chose in my user settings are "fiction" and "science"
If another user chooses "fiction", "science", "history" as their categories, I want to be able to show them all books uploaded by users that are under these three categories including all the books I uploaded since I am under the categories of "fiction" and "science"
What is the best way to implement this? I was thinking of using the acts-as-taggable-on gem but maybe this is too heavy weight and necessary. Should I just add a string column to my user model where each category is separated by a comma, so that I can split them up into an array and do queries with Active Record?
Obviously you could create your own solution for the problem but ActsAsTaggableOn is a very good solution for it. It is not such a heavy weight as other gems.
Bonus: You can go back any time if it becomes too heavy for your application. Which I doubt.
Lets say user Bob wants to keep track of his meals calories every day, which are currently breakfast, lunch, dinner, and a snack. So I make a table within rails containing four columns (breakfast, lunch, dinner, snack), and Bob puts in the calories per meal every day with no issues. A few days later, Bob decides he wants to include brunch, so my first thought would be to just write a new migration to add in the brunch column, which is fine if this is a very small scale application and such changes happen rarely, but what do I do if the app will be used by multiple people, with each person wanting to add in their own food time name?
Surely migrations are not the way, as that would require a dev to step in and modify the rails application every time, which could be many times per day. So, is there a way to do this within a rails application, is this a limitation of activerecord, or is this not the way it should be done?
For that you can add a new table e.g. "lookup_food_types" having column id and food_type. I am assuming that you are keeping the calories in a table which named as "calories". Now in calories table you need to have some modification. Its columns will be "id, user_id, lookup_food_type_id".
So in your LookupFoodType model,
attr_accessible :food_ype
has_many :calories
And in your Calory model,
attr_accessible :user_id, :lookup_food_type_id
belongs_to :lookup_food_type
In the lookup_food_types table you need to add some record initially just like
id food_type
-----------------
1 breakfast
2 lunch
3 dinner
4 snack
Now this is your basic food type for every users. If user wants to add a new food type beside the basic food_types, then you can add a new column in the calories table named as 'custom_food_type'
And in the form where user is going to add the calory, you need to show the calory as input and food_type as a select box. And a button, if user wants to add his own food type. and upon clicking that button the text field for "custom_food_type" will be visible. But the condition is that, for that user should not select any type of food type from the basic list. you can achieve it by using jquery.
Hope it will solve your problem.
I have a User model and a Company model, and I want each user to have a variable number of companies (that they have worked at), that can be pulled by e.g. current_user.companies.
The list of companies is provided by me, not the users, and is fixed.
I don't think I want
to have separate columns in the user model for e.g.
Company 1, Company 2, Company 3, as some users may have worked for 10
companies, some only 1...
to put user ids in the company model, as there are
multiple users...
How would this work in Rails associations? Do I need to have multiple company IDs in a hash in one column of the User Model to achieve this? If so how do I do this, or is there another way?