Ho un modello di database come questo:ActiveRecord unisce condizione di soddisfare tutti i rapporti
Post
has_many :votes
belongs_to :user
User
has_many :posts
has_many :votes
Vote
belongs_to :post
belongs_to :user
Quello che voglio è richiesta tutta posts
per un utente specifico che non ha votato per già.
Ho provato così:
Post.left_outer_joins(:votes).where.not(votes: { user_id: 1 })
dove 1
è solo un esempio id utente.
Il problema è che questa query sembra recuperare tutte le posts
che hanno almeno un voto in cui lo user_id
non è 1
.
Ma poiché più di un utente voterà per questi post, una volta che ci sono più di un voto, tutti gli utenti riceveranno tutti i messaggi in questo momento.
non so se joins
è l'approccio giusto ma in inglese il mio Query sarebbe:
Dammi tutti i post nessuno dei voti hanno un'user_id di 1
Is è possibile recuperare solo il posts
per un utente che non ha già votato?
EDIT:
mio schema del database dei suddetti tre tabelle:
Voti:
CREATE TABLE "votes" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
"post_id" integer,
"user_id" integer,
"created_at" datetime NOT NULL,
"updated_at" datetime NOT NULL,
"vote_type" integer DEFAULT 0);
CREATE INDEX "index_votes_on_post_id" ON "votes" ("post_id");
CREATE INDEX "index_votes_on_user_id" ON "votes" ("user_id");
Messaggi:
CREATE TABLE "posts" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
"photo_gcs_key" varchar, "photo_gcs_bucket" varchar,
"user_id" integer, "campaign_id" integer,
"created_at" datetime NOT NULL,
"updated_at" datetime NOT NULL);
CREATE INDEX "index_images_on_user_id" ON "images" ("user_id");
CREATE INDEX "index_images_on_campaign_id" ON "images" ("campaign_id");
Utenti:
CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
"uid" varchar, "display_name" varchar, "email" varchar,
"photo_url" varchar, "photo_gcs_key" varchar,
"photo_gcs_bucket" varchar, "user_name" varchar,
"created_at" datetime NOT NULL,
"updated_at" datetime NOT NULL);
Potresti per favore pubblicare lo schema dei tre tavoli precedenti? – 1000111
@ 1000111 Cosa intendi? Le prime righe mostrano le relazioni tra i tavoli. Cos'altro ti serve? – Ybrin
incolla l'output di questa query: 'mostra crea tabella' –
1000111