Foreign key search fails in Rails 3 - search

I am coding a small community in Rails 3 and I got two tables, Profiles and Contacts. When a user adds a friendship with another user it is saved in a table called Contacts which holds two columns; profile_id and friend_id.
Profile_id: This is where the users ID is saved
Friend_id: This is where the other users ID is saved
If a another user adds the user as a friend I want it to show up on the users home screen so that he can add the other user as well, but I only want it to show up if the user does not already have the other user as a friend.
I have tried the code below but it doesn't seem to work as I want it to.
#connections = Contact.where(["friend_id = ?", params[:profile_id]])
#notfriends = #connections.find_all {|profile| Contact.where(["profile_id = ? AND friend_id = ?", profile.friend_id, params[:profile_id]])}
Any ideas what is wrong? Is this the correct syntax?
UPDATE
So what I am looking to achieve is:
Get all contacts where the user is set as friend (friend_id).
Then I will would like to only get the contacts from the above query which the user does not already have as a friend (profile_id).

In line 2 of your code params[:profile_id] and profile.friend_id are necessary the same... since in your first query you search for entry where friend_id == params[:profile_id].

Related

Achievement/Badge System

I'm currently working on a website and I have used PostgreSQL, Express and Node.js technologies to make it.
I want to make an achievement system that is event based. E.g. a user makes a new post and is awarded an achievement/badge for making the first post. I was wondering what the code logic behind this would be? How do I award the user when first post is made? And the next time when user makes a new post again (second post) check if the user already has the achievement/badge for making the first post and just continue without awarding the achievement/badge again?
My database tables
The badges table looks like this:
id name description icon created_at updated_at
user_badges table
user_id
badge_id
created_at
updated_at
users table
id name etc...
Any help would be much appreciated.
You shoould have one user table that will store user data and with that you should have an column like totalQaAsked or badgeGiven like this
user's table
username, userFullName, userLoggedIN, totalQaAsked, badgeGiven
At the first time totalQaAsked would be 0 so badgeGiven would be false when user ask his first question then you should have one table that will track question asked by the user and check weather this the first question asked by the user then it will update user's table with totalQaAsked+1 and badgeGiven=true. now you'll have user's data every time you proceed then you can check easily.
Question's table
username, question, some more columns
2nd Method
You can directly query from question with user's id and check how many question asked by the user then you can give badge according to it.

How to perform these operations on entity group

In my app a user has friends. A user can send send requests and confirm friends (marked by the status attribute) and can delete friends. A user can only have X number of friends with X number of pending friends. To store this I have a Kind which doesn't exists , then there is a child Entity called Friends. Looks like this:
Friends
-Key((NonExistantKindParent,my_username), friends_username)
-status
-created_date
The key consists an ancestor which does exists, and the id is the user's username. The children entities will be the all the friends of that user. As a result of this each friendship will be stored twice, once for friend1 and again once for friend2. They are in entity groups so that they can be strongly consistent and I can perform transactions (for example if a user adds a friend it needs to write to both user who requested the friendship and the requestee friend). There are much more operations I need, but I am trying to understand how to do the following in which then I can apply that knowledge to other operations I need:
To get all of user's friends order by the date they were created (only username of friend is needed).
To get all of user's confirmed friends (status = 'confirmed').
Reading the docs at: https://googlecloudplatform.github.io/gcloud-node/#/docs/v0.30.3/datastore/query?method=select I am confused how to query entitiy groups. For a couple of those queries I need to apply a property filter and an ancestor filter it looks like but am unsure how? If I needed to just query a non-entity group the docs are quite useful.
A property filter is applied using query#filter, and an ancestor query is set with query#hasAncestor.

Issue while inserting the user in liferay

I have created a form which has basic user details and on clinking of the Save button I am inserting the user details in the "USER_" table by calling the UserLocalServiceUtil.addUser(....). Now the user is creating with out any issue. But I am not able to see some form field parameters in UserLocalServiceUtil.addUser(....) method like (Title, Gender and Date Of Birth). Now how can I save this values. Please give me some suggestions that how can I insert the following fields (Title, Gender and Date Of Birth) at the time of user creation.
Liferay uses com.liferay.portal.model.Contact entity to store the contact information.
Use com.liferay.portal.service.ContactLocalServiceUtil.addContact method to create the contact. Significant parametres:
userId - Id of the user that is creating the contact. You can use PortalUtil.getUser method to get the current user from a request.
className - "com.liferay.portal.model.User".
classPK - Id of the new user that the contact is created for.

Creating a view of users who have created nodes

Basically i want to creeate a block diaplay view,which displays a list of all the users thata have posted some nodes on the drupal website.
Oddly enough thinking about this right now it could be a little tricky. You have two possible solutions off the top of my head.
1 - Create a new view of item type Node. Your row style will obviously be set to Fields. Under which Fields to pull select the User group and then tick off the User: Name checkbox. Set your Items to display setting to 0 for unlimited results.
Under the preview you should get a ton of results looking something like:
Name: John Doe
Name: Mary Jane
Name: John Doe
Name: Anonymous
What you're seeing is the authors of all the nodes posted in your system. There will be duplication because a user in your system could be the author of multiple nodes. Unfortunately you can't just tick off the Distinct: Yes option because this only applies to nodes and not the users.
How to deal with the duplicate user name results tho? Custom theme your view by creating a custom template under Theme: information. Inside the template write some PHP code which intercepts the row results from the View query before it renders and only render distinct user names from the results. You'd have to write the logic though to determine whether a user name has already been added.
As simple as creating a new custom array, adding each row result (user name) to array but first checking to see whether it already exists in your custom array - if it does then toss it and move on to the next user name. At the end you'll have an array filled with distinct user names who have posted on your site.
voila! It works. It's not elegant but it definitely will work if built this way.
2 - Alternatively maybe you can get this module working to accomplish the same thing in a less complicated manner: http://drupal.org/project/views_customfield but I have never used it so I cannot comment on it.
Good luck. Hope that helps.
My solution was to:
Create a view of people
Add a UID field (and any other fields you want)
Create a theme.tpl.php file for the Row Style
Do a DB call on each loop through the row to search for nodes created by the supplied UID.
Here is what I have in my semanticviews-view-fields-VIEWNAME.tpl.php
<?php
//Query the Drupal DB for nodes associated to the supplied UID
$existing_nid = db_query("SELECT nid FROM {node} WHERE (type = :ctype) AND uid = :uid", array("ctype" => "CONTENT_TYPE", "uid" => $fields['uid']->content))->fetchField();
//If the supplied UID created content of the supplied type, then show their name
if ($existing_nid != FALSE) {
echo "Name:" . $fields['name']->content;
}
?>
This way only UID's that have content associated to it in the DB will be printed out, and those that don't, won't.
Hope that helps!

Two forms on the same page drupal

I have a database of clients. Before entering a new client, I want to make sure that that client is not already in the database. So I want to put a search form at the top of my page to search by client number, and client name. Further down the page, I'll have another form to enter and submit the client's information. Would this be the best way to go about something like this? How would you approach this? i'm using drupal 6.
It is better that when the user is inserting a new customer name, an autocomplete shows the names matching the characters inserted by the user; if the user wrote "Mic", and in the database there is a customer with the name "Michael Greenpeace", the autocomplete will show "Michael Greenpeace", and the user will understand there is already a record for that customer.
Even without the autocomplete (which would help the user to understand if the data for the customer has been already inserted in the database, and continue with the next customer), a user that inserted the name of an existing customer should see the existing data; this would help the user to avoid rewriting data that are already updated (customer information need to be updated, sometimes, and not only inserted).

Resources