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.
Related
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.
I have a problem with habtm on a single model in rails.
Example:
Let us say i have a User model with two roles "Student" and "teacher". User model is common for two roles. Now
Each student can be associated to many teachers
Each teacher can be associated to many students
In rails notation, their should be habtm between teacher and student
How this can be achieved with single table.
Thanks,
Aashish
It can't be done with a single table. In a many-to-many relationship, no matter what, you always need a table where you store the associations.
In your case, given the association seems to be parent/child, then you just need two tables instead of one.
How to implement it, it depends on your database structure and data organization. You should create an users_users table (as part of the habtm) and configure the references accordingly. If the user table, as it seems to be, is also used for STI, then the configuration may change a little bit.
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
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.
i am fairly new to rails and want to take advantage of the associations ActiveRecord provides in trying to set up a new project and schema. i have two models, users and questions. when a user is being set up by an administrative user, i have a form with checkboxes for a list of categories. i want these categories to map to specific questions to ask the setup user at a later time. my thinking is i have a mapping table, user_to_categories, that will allow me to get from the user to the necessary questions. but the questions table will have a primary key of question_id, so the has_many :through does not seem to work here. since many questions can be in the same category, category_id will just be another column in the questions table. any suggestions on how to set this up to take advantage of rails and active record associations?
Many to Many between user and categories and question belongs to category.
Now when you add user select categories by passing category_ids through UI,