I ask if it is good to have two references of one model in another and if it can do it how do I do it?
Problem: I have a record that will be controlled by two users (both always different users) and I need to have them see the records they have.
picture example:
example
The idea is that both can have access to the same registry and no one else can
you are trying to achieve many to many association, I would suggest you to use has_many_through association. you can read about it over here.
your tables will be like:
User:
id, email, name and other fields related to user
Box:
id, name and Fields related to boxes
User_Boxes: (Join table for mapping users with boxes, to do what you're trying to achieve)
id, user_id, box_id
so,
according to your diagram:
let the id's of boxes in box table be:
box home
box 2
box closet
box keys
Your mapping table would look like:
id box_id user_id
1 1 1
2 1 3
3 2 5
4 2 9
5 3 1
6 3 9
7 4 7
8 4 2
I hope this will solve your problem.
Related
I am building an ms access db to manage part numbers of mixtures. It’s pretty much a bill of materials. I have a table, tblMixtures that references itself in the PreMixture field. I set this up so that a mixture can be a pre-mixture in another mixture, which can in turn be a pre-mixture in another mixture, etc. Each PartNumber in tblMixture is related to many Components in tblMixtureComponents by the PartNumber. The Components and their associated data is stored in tblComponentData. I have put in example data in the tables below.
tblMixtures
PartNumber
Description
PreMixtures
1
Mixture 1
4, 5
2
Mixture 2
4, 6
3
Mixture 3
4
Mixture 4
3
5
Mixture 5
6
Mixture 6
tblMixtureComponents
ID
PartNumber
Component
Concentration
1
1
A
20%
2
1
B
40%
3
1
C
40%
4
2
A
40%
5
2
B
30%
6
2
D
30%
tblComponentData
ID
Name
Density
Category
1
A
1.5
O
2
B
2
F
3
C
2.5
I
4
D
1
F
I have built the queries needed to pull the information together for the final mixture and even display the details of the pre-mixtures and components used for each mixture. However, with literally tens of thousands of part numbers, there can be a lot of overlap in pre-mixtures used for mixtures. In other words, Mixture 4 can be used as a pre-mixture for Mixture 1 and Mixture 2 and a lot more. I want to build a query that will identify all possible mixtures that can be used as a pre-mixture in a selected mixture. So I want a list of all the mixtures that have the same components or subset of components as the selected mixtures. The pre-mixture doesn’t have to have all the components in the mixture, but it can’t have any components that are not in the mixture.
If you haven't solved it yet...
The PreMixtures column storing a collection of data is a sign that you need to "Normalize" your database design a little more. If you are going to be getting premixture data from a query then you do not need to store this as table data. If you did, you would be forced to update the premix data every time your mixtures or components changed.
Also we need to adress that tblMixtures doesn't have an id field. Consider the following table changes:
tblMixture:
id
description
1
Mixture 1
2
Mixture 2
3
Mixture 3
tblMixtureComponent:
id
mixtureId
componentId
1
1
A
2
1
B
3
1
C
4
2
A
5
2
B
6
2
D
7
3
A
8
4
B
I personally like to use column naming that exposes primary to foreign key relationships. tblMixtures.id is clearly related to tblMixtureComponenets.mixtureId. I am lazy so i would also probably abreviate everything too.
Now as far as the query, first lets get the components of mixture 1:
SELECT tblMixtureComponent.mixtureId, tblMixtureComponent.componentId
FROM tblMixtureComponent
WHERE tblMixtureComponent.mixtureId = 1
Should return:
mixtureId
componentId
1
A
1
B
1
C
We could change the WHERE clause to the id of any mixture we wanted. Next we need to get all the mixture ids with bad components. So we will build a join to compare around the last query:
SELECT tblMixtureComponent.mixtureId
FROM tblMixtureComponenet LEFT JOIN
(SELECT tblMixtureComponent.mixtureId,
tblMixtureComponent.componentId
FROM tblMixtureComponent
WHERE tblMixtureComponent.mixtureId = 1) AS GoodComp
ON tblMixtures.componentId = GoodComp.componentId
WHERE GoodComp.componentId Is Null
Should return:
mixtureId
2
Great so now we have ids of all the mixtures we don't want. Lets add another join to get the inverse:
SELECT tblMixture.id
FROM tblMix LEFT JOIN
(SELECT tblMixtureComponent.mixtureId
FROM tblMixtureComponenet LEFT JOIN
(SELECT tblMixtureComponent.mixtureId,
tblMixtureComponent.componentId
FROM tblMixtureComponent
WHERE tblMixtureComponent.mixtureId = 1) AS GoodComp
ON tblMixtures.componentId = GoodComp.componentId
WHERE GoodComp.componentId Is Null) AS BadMix
ON tblMixtures.id = BadMix.mixtureId
WHERE BadMix.mixtureId = Null AND tblMixture.id <> 1
Should return:
mixtureId
3
4
Whats left is all of the ids of that have similar components but not nonsimilar components to mixture 1.
Sorry i did this on a phone...
I have the following structure:
User model: id, ..
Event model: id, ..
UserMapEvent model: user_id, event_id
For example the UserMapEvent table might have the following records:
User_id | Event_id
1 | 1
1 | 2
3 | 1
4 | 1
I'm using this structure to save when a user likes an event.
Now, if I'm in the shows controller and I know the event's id, I can get the number of times the user liked the event like:
`likes = UsersMapEvent.where("event_id = ?", event.id).count`
How do I get, for example, the top 3 liked events? Do I have to join the 2 tables?
The only solution that I could think of is to use Event.all and for each to do the exact same thing, but it sounds stupid and I assume this is not one of those cases 'if it's stupid and it works is fine'.
What type of database operation do I have to use to get my query?
You may select top 3 liked events with next code:
Event.left_joins(:users_map_events).group(:id).order('COUNT(users_map_events.id) DESC').limit(3)
Model Material & Manager is many to many relationship,
this means Material needs to be checked by many managers,managers check many materials.
ManageMaterial is join table with attributes
material_id
manager_id
I load some data into join table like this
id manager_id material_id
---------- ---------- -----------
1 6 52
2 8 52
3 2 12
4 5 12
Now I want to show what materials are being checked, in this example I just want one item with material_id = 52, but if I do #items = ManageMaterial.all , it will return two items.
How to solve this problem?
If you want to get ManageMaterials without duplication:
#items = ManageMaterial.select("DISTINCT(material_id)")
If you want a single ManageMaterial with material_id:
#items = ManageMaterial.find_by_material_id(52)
Here's the problem: I'm using friendly_id gem to create pretty urls. No problem on a single model but I have some related model eg Category and CategoryTranslation
Category
id parent_id
1 null
2 1
CategoryTranslation
id locale_id category_id description
1 1 1 Computer Science
2 2 1 Informatica
3 1 2 Algorithms
4 2 2 Algoritmi
So, in this case I should use field description of CategoryTranslation for my urls but this is needed in finders of Category controller. Is it clear? Someone can help me? Thanks in advance.
I have two tables, one containing a list of different options users can select from. For example:
tbl_options
id_option
option
The next table I use to store which of these options the user selects. For example:
tbl_selected
id_selected
id_option
id_user
I use PHP to loop through the tbl_options table to generate a full list of checkboxes that the user can select from. When a user selects an option, the id_option and id_user are stored in the tbl_selected table. When a user deselects an option, the id_selected record is deleted from the tbl_selected table.
The challenge I am having is the best way to retrieve the full list of options in tbl_options, plus having the query indicate the associated records stored in the tbl_selected table.
I've tried LEFT JOIN'ing tbl_options to tbl_selected which provides me with the full list of options, but as soon as I add the WHERE id_user = ### the query only returns those records with values in tbl_selected. Ideally, I would like to see the results from a query as follows:
id_option option id_user
1 Apples 3
2 Oranges 3
3 Bananas
4 Pears
5 Peaches 3
This would indicate that user #3 has stored Apples, Oranges and Peaches. This also indicates that user #3 has not selected Bananas or Pears.
Is this possible using a SQL statement or should I pursue a different technique?
Your problem is that the user-restriction is applied to the whole query. To apply it only to the Join condition you need to add it to the ON clause like this:
select o.id_option, o.[option], s.id_user
from tbl_options o
left outer join tbl_selected s
on o.id_option = s.id_option and s.id_user = 3