Advertisement
Guest User

Untitled

a guest
Aug 21st, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.32 KB | None | 0 0
  1. 2.4.0 :007 > ReqCity.joins(:cities).select("cities.city")
  2. ReqCity Load (0.4ms) SELECT cities.city FROM "req_cities" INNER JOIN "cities" ON "cities"."req_city_id" = "req_cities"."id" LIMIT ? [["LIMIT", 11]]
  3. ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: cities.req_city_id: SELECT cities.city FROM "req_cities" INNER JOIN "cities" ON "cities"."req_city_id" = "req_cities"."id" LIMIT ?
  4.  
  5. ➜ db git:(master) ✗ sqlite3 development.sqlite3
  6. SQLite version 3.16.0 2016-11-04 19:09:39
  7. Enter ".help" for usage hints.
  8. sqlite> .schema
  9. CREATE TABLE "schema_migrations" ("version" varchar NOT NULL PRIMARY KEY);
  10. CREATE TABLE "ar_internal_metadata" ("key" varchar NOT NULL PRIMARY KEY, "value" varchar, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL);
  11. CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "email" varchar, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL, "password_digest" varchar, "job_titles_id" integer, "admin" boolean DEFAULT 'f', "remember_digest" varchar);
  12. CREATE UNIQUE INDEX "index_users_on_email" ON "users" ("email");
  13. CREATE TABLE "job_titles" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "job_title" varchar, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL);
  14. CREATE INDEX "index_users_on_job_titles_id" ON "users" ("job_titles_id");
  15. CREATE TABLE "requirements" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL);
  16. CREATE TABLE "cities" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "city" varchar, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL);
  17. CREATE TABLE "req_cities" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "requirement_id" integer, "city_id" integer);
  18.  
  19. class ReqCity < ApplicationRecord
  20. has_many :requirements
  21. has_many :cities
  22. end
  23.  
  24.  
  25. class Requirement < ApplicationRecord
  26. has_and_belongs_to_many :cities
  27. end
  28.  
  29. class City < ApplicationRecord
  30. has_and_belongs_to_many :requirements
  31. end
  32.  
  33.  
  34. # MIGRATION
  35.  
  36. class CreateReqCities < ActiveRecord::Migration[5.1]
  37. def change
  38. create_table :req_cities do |t|
  39. end
  40. add_column :req_cities, :requirement_id, :integer
  41. add_column :req_cities, :city_id, :integer
  42. end
  43. end
  44.  
  45. I would like to based on requirement_id in req_cities display a proper city... based on city_id. How to do it?
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement