I have a model with name Category.
I got user_id and category_id my UserCategory model.
I want to select all Categories which does not have a particular user_id.
How to do this.?
I need to make a dropdown list which do not show the Categories that the user selected before, which gets saved in UserCategory model.
In general, ActiveRecord has built-in support for NOT conditions:
Category.where.not(user_id: id)
For a many-to-many situation, given Category and User:
Category.where.not(id: current_user.categories.pluck(:id))
This will grab all categories, excluding the current user's categories by ID.
Related
I have a database where there are products grouped by categories.
When a user registers, he selects a category, which cannot be changed. The user will be able to perform some actions only on the products of that category, and some other actions on products of other categories as well. E.g. a user can comment on a product only in his category, or put a like on a product in another category.
A user with "superpowers" becomes a Moderator. He can do everything a user does, plus the ability to change something made by another user. E.g. he can modify a comment written by another user but always in reference to a product of the category he belongs to.
Then there are the Admins, who are Moderators who, however, can work independently of the category.
How can I set up the tables for this scenario?
I am designing a app and need help in my database structure. I have several categories and based on the selected category I have custom fields that the user gets. for example
category : hotels
custom fields: 1) type of hotel
2) capacity from .... to ...
3) etc
other category has another custom fields
can you suggest how you would structure the db
thank you
Dany
Maybe something like this?
categories:
id, name, etc...
custom_fields:
id, category_id, field_label, field_type, maybe some other stuff
If the custom fields are things that a user fills in and you save, you will need another table.
custom_fields_values:
id, user_id, custom_field_id, value
I have a user table which has all details like username, email id, birth date, contact no. A user has friends and these friends may belong to certain groups like for e.g close friends, family, colleagues which user has created. A friend may belong to one group or many groups or none of the groups at all.
I need to set a certain privacy related to the attributes of the user table for friends or group of friends. Like for e.g. my group of family can only view my birth date or colleagues can view only my email id but no other details like birth date or contact number.
How can I achieve this privacy?
Thanks.
Having a separate table for every "special" attribute, could lead you to have a lot of tables. On the other hand, you can use the entity-attribute-value model. This way you will have one table for users, one table for all the user's attributes, and one table for the values associated to an specific user and his attributes.
User
id
idGroup
Attribute
id
description
Value (User-Atribute)
idUser
idAttribute
value
In addition, you can relate the attribute table and the group table. By relating these two tables you can specify which attributes will be visible for each group. Don't forget to relate the user table and the group table.
Group
id
description
Access (Group-Attribute)
idGroup
idAttribute
Shouldn't you try to handle that logic on the code, instead of the database? It seems that this is related to business logic instead of the actual data model.
If you want to go forward with this approach have you considered having the private data in a separate table?
So assuming there is a showrooms table, a cars table, a trucks table and a categories table (don't mind the naming, you should get the picture). 1 Showroom can have more categories, 1 category can have more cars/trucks associated to it.
My problem is that everything is dynamically added but also, there must be default categories assigned for each showroom (ex. : main category).
Should I generate the default category (ex:showroom_id:1|category:main for categories table) each time a showroom is dynamically added? Or should I make a default_cars (no more category association) table and a custom_cars (associated to a custom categories) table ? Or any other idea?
To grant consistency of your database tables must be populated in following order:
showroom(1) -> category (2) -> cars/tracks (3)
Your sample is not comprehensive, but let me assume you mean that car should always have correct category. In this case each time when you insert new showroom you have attach this default set of categories. Done it in the same transaction. As template for categories you can use either:
NULL-reference for showroom (showroom_id: NULL |category:main )
Or stand-alone table (template_category)
I was wondering what is the best solution regarding this problem :
I have articles, and I have categories, an article belongs to a category. There are two types of categories, user defined and system defined (like "inbox","trash" etc ...)
So the question is should I have only one item for each system defined categories, and all the articles of all the users will be attached to these categories, or should I create all the systems defined categories when creating the user, and attached these categories to the user ?
The first solution will result in a lot of articles in each system defined categories, and the second solution will result in a lot of "redundant" item in category. Which solution is the best ?
It's your choice. Both approaches has its pros and cons.
If you go with the first solution, your select statement will have a WHERE clause similar to this:
WHERE (category.type = 'SYSTEM' OR category.user_id = :USER_ID)
The second solution will simplify your select statements:
WHERE category.user_id = :USER_ID
The second solution will make it more complex to add a user. You need to create the system defined categories for the user:
INSERT INTO CATEGORY (user_id, category_name, ...) SELECT :USER_ID, system_category, ... FROM SYSTEM CATEGORY;
And if you add a new system category you need to insert it for all users.