I have added a multiple select box like this one to my form HTML multiple select box.
I have a User object and a Personality object. A user can have 0 or more personalities and a personality can "belong" to 0 or more users. For this many to many relation I created a third model UserPersonality to store the ids of the User and Personality.
My User and Personality models have their corresponding has_many through indications. My question is, how can I make the helper add the association to the UserPersonality table through a select box like the one on the left from the link provided?
If a user adds a personality from the right select box to the left box I want to add a record such as:
UserId PersonalityId
1 3
To connect a User with the Personality that is selected.
Thanks.
It was easier to solve the problem using the has_and_belongs_to_many association vs the has_many through
This is because I'm only using the join table for the ids and with this association I can simply do
u=User.find(1)
p=Personality.find(1)
u.personalities << p
To associate them and populate the joint table: users_personalities
Related
I have models called Rep and Bill. What I want to do is to populate two fields on the Bills form called sponsor and cosponsor. I want to populate the sponsor and cosponsor fields using a select box with a list of all the Reps that can accept multiple selections. I know how to set up the select box with the reps populating the select with multiple true. My question is how do I set up this type of association? Should I create separate models called sponsor and cosponsor and is this the appropriate situation to use has_many through or polymorphic associations?
It wasn't as complex as I had initially thought, I was able to use has_many through relationship in order to have the join table be sponsor or cosponsor.
I'm not sure whether I've accurately reflected my aim in the title, but I'll explain more here.
In my app I have Companies, and Companies has_many Key_Contacts.
Companies also has_many Sales_Opportunities.
I would like the user to be able to select some of the Key_Contacts that belong_to the Company and associate them with a specific Sales_Opportunity. I would also like the user to be able to add a Key_Contact that is not associated with any Sales_Opportunity.
The aim for this is that I can show the specific Key_Contacts that are involved in one Sales_Opportunity view on the Sales_Opportunity page, but not all of them.
Is it as simple as adding a sales_opportunity_id to the Key_Contacts model, but not setting up the "belongs_to" and "has_many" relationships? Or is there a more "official Rails" method to achieve my goal?
If I am reading this right, then all you need to do is add another has_many :key_contacts relation to your SalesOpportunity model (and belongs_to :sales_opportunity in your KeyContacts model). Then relate all contacts belonging to a specific sales opportunity.
(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.
For practice I'm writing a shopping website where we have tables User and Item. A user obviously has_many items (when they are added to their basket), but the item, it belongs_to a User, even though many users will have the same item in their basket?
Furthermore, what if I want a list of items a user has added to their basket, but also a list of items they have viewed (for making suggestions based on searches), would it be better to have some 'through' tables: Basket and Viewed?
When you have this many-to-many relationships, you can use the HABTM schema:
Class User...
has_and_belongs_to_many :items
However, most of the time webshops use orderlines to keep up with items that users are purchasing. This means that an 'user' 'has_many' 'orderlines', an 'item' 'has_many' 'orderlines', an 'orderline' 'belongs_to' an 'user' and to an 'item'.
And maybe your orderlines will just be copies of items, and won't have a direct link because you don't want to alter the orderline after they have been processed. It really depends on the focus of your shop which scheme suits your needs.
Try to find some examples on the web and think about how you want to handle items, orders and baskets.
I'm used to separate things that are not the same, even if the relationship is one-to-one. So first of all I would recommend users from baskets (1:1-relationship).
After that a basket contains many items and items can be in multiple baskets (m:n-relationship). Make sure, that maybe a user likes to buy the same item multiple times.
views can be realised as a linking table between users and items: users have many views and items have many views, but one view is always linked to exactly one user and one item.
Given the parent / child HABTM relationship:
class List < ActiveRecord::Base
has_and_belongs_to_many :items
end
class Item < ActiveRecord::Base
has_and_belongs_to_many :lists
end
I need to set up a UI to add Items (children) to a List (parent) from the List create / edit form. The wrinkle is that there are far too many Items records to use checkboxes. An initial thought is to have a popup window that allows the user to browse the collection of Items to find the records to add, but I'm not sure how that would work with new List records (where the parent ID would not yet exist). I'm looking for a solution that will make it easy for the user to navigate the large collection of child records and add them to a parent record in an intuitive manner with the minimum amount of clicks required.
Users will need the ability to browse the Item records to find acceptable choices, so an auto-complete text box which would force users to search for records they want will not work in this case. The child Item records are organized with multiple attributes (e.g. title, author, genre, rating), so my original plan was to set up a table with the Item records with AJAX filter and sort to allow the user to narrow down the Item collection to the desired elements, and to then somehow add selected records to a List. Users do not need to be able to define new Item records while creating a list (i.e. when creating a List, the user will simply select multiple Items from the existing collection).
I like the auto-completing text-box for lookups like this; it'll depend on if your child-data set is intuitive enough for users to guess.