Rails associations: hash of ids within one column needed? - ruby-on-rails

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?

Related

set attributes in a has_many_though relationship in Rails

I have three models: Company, User and Employment. Each Company has many users though their Employments, and a user might belong to several companies through his employments.
Now, the tricky part: let's say my User1 belongs to 2 companies. He is SUPER_ADMIN in the first compnay, but just BASIC_USER in the second one. What would be the best way to define his roles ?
I used to have a simple has_many relationship between Company and User, which allowed me to simply set a is_admin attr on my User, but obviously this won't do with the new HMT relationship.
I thought of defining an array of IDs for each company, that would include the IDs of each admin user, but I'm pretty sure there is a cleaner way around.
In above scenario, you can use intermediate table i.e. Employment table to save all details of a user and it's associate company.
As Employment table will have ids of both user & company it will be easy for you to keep extra information related to user and company. Just add role column in this and use this to get information

Categories assigned to user model - rails

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.

What is the best practice in handling multiple checkboxes in Rails?

(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.

Best way to recommend Products to Users based on Interest?

Let's say that each Product has a category. I want to ask the Users to select several categories that the user is interested in, and find the Products that have the same category. This is similar to what Quora, Stumbleupon, and Pinterest all do.
What would be the best way to set this database structure in Rails? Should I create 3 tables: User, Product, and Category, and make the relations
User has many Categories & Product has many Categories?
The problem I see with this is doesn't it create, rather than reference, a new instance of Categories to each row of Users and Products?
*extra: What if I wanted subcategories? For example, if the user chose Technology, it could further ask to choose between web dev, mobile dev, hardware, etc.
You could do that kind of 'recommendation' pretty easily.
Something like this should work (N.B.: I did not test this code, but it is right in spirit):
def recommended_products
joins(:categories, :products).where("product_id not in (?)", self.products)
end
Explanation of each bit:
joins(:categories, :products): this does a SQL join of users, products, and categories. This gives you a 'table' where each user-product-category combination is in it's own row.
.where("product_id not in (?)", self.products): adds a SQL where clause to filter out all the rows that have products in the current user's list of products.
The associations are not a problem. They don't create any new instances by themselves, only if you write code that creates new instances yourself.
As for sub categories, I think you'll do better to make that it's own question, as it's easily a whole post in itself.

Rails - a model that can have two different sub items

I have a standard devise user model with the usual fields.
This is for a situation where people are either looking for a place to stay, or they have a place to stay. So I have two categories of user that a person can be. These two categories are very distinct (i.e. a person looking for a place to stay will have very different fields to a person who has a place to stay).
So a User has:
User: name, email, password, profile_id
A User can also have a Profile (i.e. they are looking for a house).
Profile: age, sexuality, religion, occupation
That's what I have at the moment. Now I need to change that slightly, so a User can have a profile OR... they can have a House (i.e. they have a house and are looking for more people):
House: price_per_week, address, etc
How best to model this in ActiveRecord? Polymorphic association of some kind?
I've found in general that polymorphic relationships don't work well over time if the objects they are modeling are even mildly different. For your case I'd recommend keeping the two objects separate.
In general, the best way is to consider the way you want to retrieve the data. For example, I'd imagine you want to access both:
#user.house
or
#user.profile
So I'd recommend beginning by setting up relationships (that can be optional) between the users table and both the profiles and houses table. I'd also add a type field that can be used to determine which of the two types the users are.
This allows users to be of either type and allows them to have both a profile and a house.
So both houses and profiles belong_to users, and users have_many (or have_one) houses and profiles

Resources