Generate Unique id for each Employee of each Company using Rails5 - ruby-on-rails

I want to create unique id for each Employee of company. For example currently I am getting like this.
Company has many employees and employee belongs_to company
company1= employee1
company1 = employee2
company1 = employee3
company2 = employee4
company2 = employee5
So, here employee id's are not getting changed while company_id is changed.
So, I want something like this:
For each company employee_id should start with 1.
like
Company1 = employee1
company1 = employee2
company1 = employee3
company2 = employee1
company2 = employee2
Could anyone please help me how to do that in Rails5?
employee table
id name position
1 aaa eg_position
2 vvv qqq
company table
id name
1 xxx
1 yyy
so present its storing like
xxx - 1, yyy-2
I want like this xxx-1.yyy-1

Use uniqueness validation based on scope of composite key. employee_id and company_id
validates :employee_id, uniqueness: {scope: [:employee_id, :company_id]}

We have to be very clear.
I am assuming that you have an employees table where all employees of all different companies are stored.
If that is the case, then you cannot have two primary keys which are the same. You cannot do this.
But what you can do is have another field, perhaps employee number (which is different to id) which can be the same across different employees in different companies.
Personally i don't like the idea of a composite primary key.
What I would do is create another column in your table called: employee number. And use that to store what you call: "id"
I hope this helps.
edit:
Further explanations.
Every row in a table has a column called "id". this is a special name. this is the name of the "primary key". Don't change that name. Two rows in the same table cannot have the same "id" or primary key. You can't do it. It's fundamental to a database. you can't have the same primary key in two rows of the same table - for mysql, psql, Access etc.
but you want to identify two employees with the same "identification number". That's fine. You can do it. Just don't call the employee identification number: "id" because that won't work. You can call it "employee_no" and that would work fine.
show my your database schema and we can further clarify.
hope this helps.

Related

How to query DynamoDB with for multiple ids eg: student_id in [1,2,3] #rails

I have a table in DynamoDB.
student_id, name, age, address partition_key: student_id, table_name: students
lets say student ids are 1,2,3 etc...
I wanted to query students based on Ids.
eg sql select * from students where id in (1,2,3)
How to do the same in DynamoDB
Please help me with the query params.
I tried
params = {
:table_name=>"students",
:key_condition_expression=>"student_id IN :student_id",
:expression_attribute_values=>{":student_id"=> [1,2,3]}
}
DynamoDB does not allow you to apply conditionals around the partition key (e.g. PK in [1,2,3]), you need to specify it explicitly. However, you can perform various condition operations around sort keys (<,>, begins_with, etc).
If you can identify the primary key, which I'm assuming is your student_id and you want students with id 1, 2, and 3, then you can use the client batch_get_item().

Clean and concise way to find active records that have the same id as another set of active records

I have a table called shoppers and another table called Users. I have a shopper_id which is the foreign key in the Shoppers table and refers to the primary key id in the Users table.
I ran a query called #shoppers = shoppers.where("some condition")
This allowed me to get a set of shoppers who satisfy the condition. Next I would like to select those Users who have the same id as the shopper_id as the individual objects in #shoppers.
I know I could do this by writing a loop, but I am wondering if ruby on rails allows me to write a Users.where condition that can help me obtain the subset of user objects with the same id as shopper_id arranged in ascending order by the name field in the Users table.
Any ideas?
Try this.
#shoppers = Shopper.where("some condition")
#users = User.where(id: #shoppers.collect(&:shopper_id)).order('name asc')

Join multiple FK values in one table

I'm writing some access queries and I'd like some help on a particular query. I'm still very new to SQL. Here is a simplified version of my tables:
Project Details
---------------
projectID (PK)
projectStartDate
projectEndDate
projectName
managerID (FK)
leadID (FK)
coleadID (FK)
Employee
--------
empID (PK)
empName
The managerID, leadID, and coleadID all correspond to an empID. I'd like to retrieve the Project Details table, but replace the IDs with the names of the employees. I've successfully been able to do this for one FK at a time using an inner join, but can't figure out a way to accomplish this for all roles.
It would also be nice to be able to change the attribute names on the results to managerName, leadName, and coleadName.
Thanks!
It's the same way, you probably have done it for one ID:
SELECT pd.*
, emp_m.empName ManagerName
, emp_l.empName LeadName
, emp_c.empName ColeadName
FROM ProjectDetails pd
, Employee emp_m
, Employee emp_l
, Employee emp_c
WHERE pd.managerID = emp_m.empID(+)
AND pd.leadID = emp_l.empID(+)
AND pd.coleadID = emp_c.empID(+)
The (+) is for an outer join, so it will select all records of the ProjectDetails table, no matter if it can match the manager, lead or colead.

Rails Creating third table from 2 tables

How can I merge column from one table into another table and save it as a variable, what would be the most efficient way of accomplishing this. Here is an example:
Table 1:
ID First Name LastName contact_id
Table 2:
Id Phone Address email
Id from table 2 is a foreign key of contact_id
I need my third table to look like this:
Id Phone Address Email person_id
person_id corresponds to ID from table 1.
What would be the most efficient way using rails commands to accomplish this result so that it gives me the third table?
Why not just add person_id to table 2...
Table1Model.all.each do |model|
x=Table2Model.find(model.contact_id)
x.person_id = model.id
x.save
end

Connecting Id field with name field

Am having a table with quetion_id , nominees and vote_count. In which the values for question_id and nominees are prepopulated from other tables with vote_count as zero.
If the users select some nominees the vote count should be incresed by one. The problem is How to connect the question_id and nominees like for this question_id this nominee is selected .
can some one give example for this situation..
I'll answer based on this scenario:
So you have a...
1) User
who can...
2) Vote
for a...
3) Nominee
And it's a given that MANY users can vote for MANY nominees.
You probably aready have tblUser and tblNominee - so you need a link table that can contain the votes (tblUserNomineeVote).
tblUserNomineeVote has fields for UserId and NomineeId, and therefore registers a vote. You may need to add constraints depending on how many votes a user can register etc.
You can then use:
SELECT
tblNominee.Name,
COUNT(*)
FROM
tblNominee
INNER JOIN
tblUserNomineeVote ON tblUserNomnieeVote.NomineeId = tblNominee.NomineeId
GROUP BY
tblNominee.Name

Resources