Request on PostgreSQL with Rails and Squeel - ruby-on-rails-3

I was advised to create a new question based on my previous one so here it is :
I have on my website a list of product, each user can create his own savings based on these products. So several users can create their own savings based on the same product. So when one user create a saving I add in the database a saving with the user_id and the product_id he refers to. Now, on one page I want to display all the savings from the other users (excluding current_user) but, as many user can create a saving based on the same product I want to display these only once.If several users create a saving based on product X I want it only displayed once,thanks
The saving table has these field :
id
product_id
user_id
price
saved
What I did is what is posted on this question :
Squeel request and Heroku, Postgres: Error using GROUP BY and ORDER
I cannot get it working properly on heroku (postgres). If anyone can help me writing the query for Rails or for Squeel that would be awesome thanks !

Well Craig thanks for your help I ended up doing that and it's working on PG in local !
Saving.select("DISTINCT ON (savings.product_id) * ").where{product_id.not_in(current_user_savings.select{product_id})}.group("savings.user_id, savings.updated_at, savings.id, savings.product_id, savings.price,savings.wishlist_id, savings.saved, savings.created_at")
Having that issue made me switch my db from mysql to pg in dev so no more surprise in heroku !

Related

Wordpress SQL request to delete all user subscriber without publication

I have 3000 users and 2800 user without publication, so i want to delete all spam user with sql. thanks
Your question isn't very clear -
But if you mean "delete all users who have never posted anything" - Then
DELETE FROM `wp_users`
WHERE ID NOT IN
(SELECT DISTINCT post_author FROM `wp_posts`);
would do it... remember to back up your data first.
This will run through all the posts, make a list of the user IDs that ARE authors and then delete anyone who isn’t on the list from your wordpress users. Since custom post types are stored in wp_posts, it’ll be checking the author against all of those too.

Auto Delete Old Wordpress Posts in Certain Categories via SQL

I found the following SQL script here in another thread:
DELETE FROM `wp_posts`
WHERE `post_type` = 'post'
AND DATEDIFF(NOW(), `post_date`) > 120
This script will find and delete all posts older than 120 days.
However, I'm wondering if this can also be done in certain categories only.
I'd like to keep all the old posts in some categories, but delete all old posts in others.
Also, if so, is there a way to make this script run every day?
I'm extremely green when it comes to this sort of thing, so any help will be greatly appreciated.
You'd better use wp_delete_post ( http://codex.wordpress.org/Function_Reference/wp_delete_post ) function to do what you wanna do, because there a lot of relationships beetween posts comments etc. in the database. => http://codex.wordpress.org/images/9/9e/WP3.0-ERD.png
To run this script everyday you have to make a CRON job on your server :
http://www.thesitewizard.com/general/set-cron-job.shtml
hope it helps

Need a strategy for an efficient autocomplete in Rails across multiple attributes

I have a form for submitting an order, and I need an autocomplete field that searches across three attributes in the associated customer model: first name, last name, and customer_number (as opposed to customer.id). I know about the rails3-jquery-autocomplete gem found here http://github.com/crowdint/rails3-jquery-autocomplete, and got it working well, but a question has occurred to me -- is there a more efficient way to make the autocomplete work without having to query the db every time?
The other solution that occurred to me is to create a new indexed attribute in the customer model -- call it autocomplete_data. Whenever a new customer is added via the usual new customer form, an :after_create callback could populate the field. Would this speed up the performance? Or am I overthinking it?
UPDATE
I'm embarassed to say that I just didn't search hard enough the first time around -- I think this actually answers my question:
Rails: Efficiently searching by both firstname and surname

Selecting specific joined record from findAll() with a hasMany() include

(I tried posting this to the CFWheels Google Group (twice), but for some reason my message never appears. Is that list moderated?)
Here's my problem: I'm working on a social networking app in CF on Wheels, not too dissimilar from the one we're all familiar with in Chris Peters's awesome tutorials. In mine, though, I'm required to display the most recent status message in the user directory. I've got a User model with hasMany("statuses") and a Status model with belongsTo("user"). So here's the code I started with:
users = model("user").findAll(include="userprofile, statuses");
This of course returns one record for every status message in the statuses table. Massive overkill. So next I try:
users = model("user").findAll(include="userprofile, statuses", group="users.id");
Getting closer, but now we're getting the first status record for each user (the lowest status.id), when I want to select for the most recent status. I think in straight SQL I would use a subquery to reorder the statuses first, but that's not available to me in the Wheels ORM. So is there another clean way to achieve this, or will I have to drag a huge query result or object the statuses into my CFML and then filter them out while I loop?
You can grab the most recent status using a calculated property:
// models/User.cfc
function init() {
property(
name="mostRecentStatusMessage",
sql="SELECT message FROM statuses WHERE userid = users.id ORDER BY createdat DESC LIMIT 1,1"
);
}
Of course, the syntax of the SELECT statement will depend on your RDBMS, but that should get you started.
The downside is that you'll need to create a calculated property for each column that you need available in your query.
The other option is to create a method in your model and write custom SQL in <cfquery> tags. That way is perfectly valid as well.
I don't know your exact DB schema, but shouldn't your findAll() look more like something such as this:
statuses = model("status").findAll(include="userprofile(user)", where="userid = users.id");
That should get all statuses from a specific user...or is it that you need it for all users? I'm finding your question a little tricky to work out. What is it you're exactly trying to get returned?

MySQL joins for friend feed

I'm currently logging all actions of users and want to display their actions for the people following them to see - kind of like Facebook does it for friends.
I'm logging all these actions in a table with the following structure:
id - PK
userid - id of the user whose action gets logged
actiondate - when the action happened
actiontypeid - id of the type of action (actiontypes stored in a different table - i.e. following other users, writing on people's profiles, creating new content, commenting on existing content, etc.)
objectid - id of the object they just created (i.e. comment id)
onobjectid - id of the object they did the action to (i.e. id of the content that they commented on)
Now the problem is there are several types of actions that get logged (actiontypeid).
What would be the best way of retrieving the data to display to the user?
The easiest way out would be gabbing the people the user follows dataset and then just go from there and grab all other info from the other tables (i.e. the names of the users the people you're following just started following, names of the user profiles they wrote on, etc.). This however would create a a huge amount of small queries and trips to the database in a while loop. Not a good idea.
I could use joins to retrieve everything in one massive data set, but how would I know where to grab the data from in just one query? - there's different types of actions that require me to look into several different tables to retrieve data, based on the actiontypeid...
i.e. To get User X is now following User Y I'd have to get my data (User Y's username) from the followers table, whereas User X commented on content Y would need me to look in the content table to get the content's title and URL.
Any tips are welcome, thanks!
Consider creating several views for different actiontypeids. Union them to have one full history.