Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 2.4.0 :007 > ReqCity.joins(:cities).select("cities.city")
- ReqCity Load (0.4ms) SELECT cities.city FROM "req_cities" INNER JOIN "cities" ON "cities"."req_city_id" = "req_cities"."id" LIMIT ? [["LIMIT", 11]]
- 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 ?
- ➜ db git:(master) ✗ sqlite3 development.sqlite3
- SQLite version 3.16.0 2016-11-04 19:09:39
- Enter ".help" for usage hints.
- sqlite> .schema
- CREATE TABLE "schema_migrations" ("version" varchar NOT NULL PRIMARY KEY);
- CREATE TABLE "ar_internal_metadata" ("key" varchar NOT NULL PRIMARY KEY, "value" varchar, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL);
- 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);
- CREATE UNIQUE INDEX "index_users_on_email" ON "users" ("email");
- CREATE TABLE "job_titles" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "job_title" varchar, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL);
- CREATE INDEX "index_users_on_job_titles_id" ON "users" ("job_titles_id");
- CREATE TABLE "requirements" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL);
- CREATE TABLE "cities" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "city" varchar, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL);
- CREATE TABLE "req_cities" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "requirement_id" integer, "city_id" integer);
- class ReqCity < ApplicationRecord
- has_many :requirements
- has_many :cities
- end
- class Requirement < ApplicationRecord
- has_and_belongs_to_many :cities
- end
- class City < ApplicationRecord
- has_and_belongs_to_many :requirements
- end
- # MIGRATION
- class CreateReqCities < ActiveRecord::Migration[5.1]
- def change
- create_table :req_cities do |t|
- end
- add_column :req_cities, :requirement_id, :integer
- add_column :req_cities, :city_id, :integer
- end
- end
- 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