Any one have implemented a way manage to user friends(friendship in two way) and followers in database.
means what I want to achieve:
1) user1 send a connection request to user2.
2) then user2 accept user1 as friend or follower or reject it.
3) if user2 accept user1 as friend then this friendship is two way friendship.
what I am thinking to handle this:
1) I'll create a friendship table.
2) relationship column that maintain a user is follower/friend.
3) if user is friend then I'll create two entry to maintain friends relationship from both end.
Could you please suggest me best approach(gems/plugin) to handle this case.
You should create friendship with following fields
1] user_id
2] friend_id
3] status
At first when user send a request for friendship status must be Pending and when friend accept/reject the request it should be change to the Accepted/Rejected
You can create a table with columns - USER_A, USER_B, RELATION. Now, you can create a constant or enums to define the relation between USER_A and USER_B. For e.g. 0 represents no relation, 1 represents USER_A -> USER_B ( User A follows User B ), 2 represents USER_B -> USER_A ( User B follows User A ), and 3 presents USER_A <-> USER_B ( that means both way. )
You can then update the same row if you don't require history otherwise you can add one more row as the relation changes.
Hope that works!
You can check out socialization gem that provide likes, following and mentions to your ActiveRecord models.
Related
I have this table:
User
Name
Role
Mason
Engineer
Jackson
Engineer
Mason
Supervisor
Jackson
Supervisor
Graham
Engineer
Graham
Engineer
There can be exact duplicates (same Name/Role combination). Ignore comments about primary key.
I am writing a query that will give the distinct values from 'Name' column, with the corresponding 'Role'. To select the corresponding 'Role', if there is a 'Supervisor' role for a name, that record is returned. Otherwise, a record with the 'Engineer' role should be returned if it exists.
For the above table, the expected result is:
Name
Role
Mason
Supervisor
Jackson
Supervisor
Graham
Engineer
I tried ordering 'Role' in descending order, so that I can group by Name,Role and pick the first item - it will be a 'Supervisor' role if present, else 'Engineer' role - which matches my expecation.
I also tried doing User.select('DISTINCT ON (name) \*).order(Role: :desc) - I am not seeing this clause in the SQL query that gets executed.
Also, I tried another approach to get all valid Name, Role combinations and then process it offline iterating the result set and using if-else to decide which row to display.
However, I am interested in anything that is efficient and does not over do this handling.
I am new to Ruby and therefore reaching out.
If I wanted to do this in pure SQL, I would have to use GROUP BY.
SELECT Name, MAX(Role) FROM User GROUP BY Name
So one method would be to execute this SQL statement against the base connection.
ActiveRecord::Base.connection.execute("SELECT Name, MAX(Role) FROM User GROUP BY Name")
That would provide exactly the data you need, though it wouldn't be returned as ActiveRecord models. If you need those models then I would use find_by_sql and do an inner join to provide the records.
User.find_by_sql("SELECT User.* FROM User INNER JOIN (SELECT Name AS n, MAX(Role) AS r FROM User GROUP BY Name) U2 WHERE Name = U2.n AND Role = U2.r")
Unfortunately that would provide both records for Graham.
I need help on devise authentication (https://github.com/heartcombo/devise) about logins.
My db design has 3 tables that need 2 joins, so it can use any of a user's emails for login, using a single password.
profiles table
id
name
emails table
profile_id - foreign key from profiles table using has many
email
users table
profile_id - foreign key from profiles table using one-to-one relationship
encrypted_password
At the moment, I can only set 1 join in conditions.
You didn't provide any details except that 2 tables have a FK to a third. So I cannot provide much detail in response. But joining all 3 is a trivial exercise. it'll come in the format of:
select
from profiles p
join emails e on e.profile_id = p.profile_id
join users u on u.profile_id = p.profile_id
where e.email = <email_address_parameter>
and encryption_routine(<password_value_parameter>) = u.encrypted_password;
Assume you have the correct model relations.
if(email = Email.find_by(email: params[:email])).present?
if(email.profile.users.where(encrypted_password: encrypt(params[:password])).count > 0)
# login success
else
# wrong password
end
else
# email not exists
end
I have database table with userInfo and i have a field to store user tickets.A user can have multiple tickets Say , user1 tickets: {t1,t2,t3}
user2 tickets:{t1,t2}.I want to search user with a particulat ticket (ex : select * from users where tickets in(t1) in mysql).Please help me
Perhaps: SELECT * FROM users WHERE tickets LIKE '%t1%'
the % wildcard matches any number of characters.
Let me know if this worked for you.
I have these tables:
broadcast
id
name
email
id
broadcast_id
user_id
subject
email_open
id
email_id
user_id
I want to keep a count of the email_open records in my broadcast table.
Is the most efficient way of doing this by having a broadcast_id in my email_open table? If it is.. then I know I can just do this in my email_open model:
belongs_to :broadcast, counter_cache: => true
Then, I add a email_open_count to my broadcast table... but I'm wondering if there's a way to do it without doing this.
Also, multiple users can have repeated records in email_open.. how do I make the count be of distinct user_id?
For example, user_id 1 can open an email 5 times but I just want the email_open_count to be 1.
Thanks
So as per your example, if user_id 1 can open an email 5 times but You just want the email_open_count to be 1.You can the same user_id to email or whichever table you want only if that user opens an email but if the user has already open don't save it again.
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