How to add a pseudonymization and a search function? - ruby-on-rails

I'm new to Ruby on Rails and I got two problems where I need some help:
I got the tables "cases" and "users". The table case includes the column "first name" and "last name". Now I want to add a unique string (pseudonymization) to each case for a special kind of identification without using the ID. This string should be build from the third letter of the first name and the total amount of letters plus the third letter of the last name and again the total amount of letters of the last name. E.g. for the name "Bill Smith" the string would be: L4I5.
Now the "users" should be able to find a specific case using that created special string. There should be a kind of a searching field where they can type in the string and click on search. Then the case with all the parameters should be shown.
What is the best way, to implement these two functions/features? I'm using rails 6.0.3.4 and ruby 2.7.0.
For better understanding see my schema.rb below.
ActiveRecord::Schema.define(version: 2021_01_28_100706) do
create_table "cases", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci", force: :cascade do |t|
t.string "first_name"
t.string "last_name"
t.string "gender"
t.date "birthdate"
t.string "place_of_residence"
t.string "diagnosis"
t.bigint "user_id"
t.datetime "confirmed_at"
t.datetime "created_at", precision: 6
t.datetime "updated_at", precision: 6, null: false
t.bigint "diagnosis_id"
t.bigint "district_id"
t.bigint "report_id"
t.index ["diagnosis_id"], name: "index_cases_on_diagnosis_id"
t.index ["district_id"], name: "index_cases_on_district_id"
t.index ["report_id"], name: "index_cases_on_report_id"
t.index ["user_id"], name: "index_cases_on_user_id"
end
create_table "diagnoses", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci", force: :cascade do |t|
t.string "illness"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end
create_table "districts", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci", force: :cascade do |t|
t.string "name"
t.string "place"
t.integer "postal_code"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.bigint "state_id", null: false
t.index ["state_id"], name: "index_districts_on_state_id"
end
create_table "reports", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci", force: :cascade do |t|
t.bigint "district_id"
t.text "comment"
t.datetime "date"
t.bigint "user_id"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["district_id"], name: "index_reports_on_district_id"
t.index ["user_id"], name: "index_reports_on_user_id"
end
create_table "states", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci", force: :cascade do |t|
t.string "titel"
t.string "abbr"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end
create_table "users", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.integer "role"
t.string "first_name"
t.string "last_name"
t.bigint "district_id"
t.bigint "state_id"
t.index ["district_id"], name: "index_users_on_district_id"
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
t.index ["state_id"], name: "index_users_on_state_id"
end
end

You could add a column to the case table and a callback in the model to set it how you want. And to search just add a scope
#migration
add_column :cases, :new_column, :string, index: true
#model
class Case < ApplicationRecord
before_create :set_new_column
scope :by_new_column, ->(term) { where('new_column = ?', term) }
...
private
def set_new_column
self.new_column = "#{first_name[0]}#{last_name.length}"
end
end
Then in your controller or where ever, you could use like:
Case.by_new_column("C5")

Related

Make a class method to count the comments

I am learning the Ruby on Rails and I would like to make a class methods to count the article comments.
I am creating this class method
class_methods do
def comment_count
end
end
And this is my schemas
create_table "articles", force: :cascade do |t|
t.string "title"
t.text "body"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.string "status", default: "public"
end
create_table "comments", force: :cascade do |t|
t.string "commenter"
t.text "body"
t.integer "article_id", null: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.string "status"
t.index ["article_id"], name: "index_comments_on_article_id"
end
add_foreign_key "comments", "articles"

Associations for statistics

I'm new to RoR and I need some help with associations. I'm using rails 6.0.3.4 and ruby 2.7.0.
Users can create cases and cases belongs to a certain district. Districts belongs to a state. It has to be that way, because cases can't belongs to a state.
Now I want to show the number of cases for a certain diagnosis for each state. I have to use district, to get all the cases for a state. How should I build the where(...) condition?
<!-- State -->
<div class="card">
<div class="card-body">
<h5 class="card-title">State</h5>
<p><%= State.find(1).titel%> (<%= #diagnosis.cases.where(...).count %>)</p>
</div>
</div>
My Models
case.rb
class Case < ApplicationRecord
before_create :set_pseud
belongs_to :user
belongs_to :diagnosis
belongs_to :district
belongs_to :report, optional: true
end
district.rb
class District < ApplicationRecord
has_many :users
has_many :cases
has_many :reports
belongs_to :state
end
state.rb
class State < ApplicationRecord
has_many :districts
has_many :users
end
For better understanding my schema.rb:
ActiveRecord::Schema.define(version: 2021_02_11_140244) do
create_table "cases", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci", force: :cascade do |t|
t.string "first_name"
t.string "last_name"
t.string "gender"
t.date "birthdate"
t.string "place_of_residence"
t.string "diagnosis"
t.bigint "user_id"
t.datetime "confirmed_at"
t.datetime "created_at", precision: 6
t.datetime "updated_at", precision: 6, null: false
t.bigint "diagnosis_id"
t.bigint "district_id"
t.bigint "report_id"
t.string "pseud"
t.index ["diagnosis_id"], name: "index_cases_on_diagnosis_id"
t.index ["district_id"], name: "index_cases_on_district_id"
t.index ["pseud"], name: "index_cases_on_pseud"
t.index ["report_id"], name: "index_cases_on_report_id"
t.index ["user_id"], name: "index_cases_on_user_id"
end
create_table "diagnoses", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci", force: :cascade do |t|
t.string "illness"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end
create_table "districts", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci", force: :cascade do |t|
t.string "name"
t.string "place"
t.integer "postal_code"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.bigint "state_id", null: false
t.index ["state_id"], name: "index_districts_on_state_id"
end
create_table "reports", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci", force: :cascade do |t|
t.bigint "district_id"
t.text "comment"
t.datetime "date"
t.bigint "user_id"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["district_id"], name: "index_reports_on_district_id"
t.index ["user_id"], name: "index_reports_on_user_id"
end
create_table "states", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci", force: :cascade do |t|
t.string "titel"
t.string "abbr"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end
create_table "users", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.integer "role"
t.string "first_name"
t.string "last_name"
t.bigint "district_id"
t.bigint "state_id"
t.index ["district_id"], name: "index_users_on_district_id"
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
t.index ["state_id"], name: "index_users_on_state_id"
end
end
You should add a further association into state:
has_many :cases, through: :districts
Rather than finding your state in the view, you should do that in the controller and pass it to the view in an instance variable:
#state = State.find(params[:id])
I've assumed you're using a show action here rather than manually coding the state ID for some reason.
You can then do something like this:
#state.cases.where(diagnoses: { id: #diagnosis.id }).count
Or if you prefer, you can skip the .id on #diagnosis:
#state.cases.where(diagnoses: { id: #diagnosis }).count

has many through association rails

Users can upload tracks and create playlist. I have a model for playlist and i have a model for playlist_track which is for users that can save to the playlist. I can create a new playlist but how do i add the tracks to the playlist in the views?
ActiveRecord::Schema.define(version: 2018_12_06_050857) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
create_table "active_storage_attachments", force: :cascade do |t|
t.string "name", null: false
t.string "record_type", null: false
t.bigint "record_id", null: false
t.bigint "blob_id", null: false
t.datetime "created_at", null: false
t.index ["blob_id"], name: "index_active_storage_attachments_on_blob_id"
t.index ["record_type", "record_id", "name", "blob_id"], name: "index_active_storage_attachments_uniqueness", unique: true
end
create_table "active_storage_blobs", force: :cascade do |t|
t.string "key", null: false
t.string "filename", null: false
t.string "content_type"
t.text "metadata"
t.bigint "byte_size", null: false
t.string "checksum", null: false
t.datetime "created_at", null: false
t.index ["key"], name: "index_active_storage_blobs_on_key", unique: true
end
create_table "playlist_tracks", force: :cascade do |t|
t.integer "playlist_id"
t.integer "track_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "playlists", force: :cascade do |t|
t.string "title"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "plays", force: :cascade do |t|
t.integer "user_id"
t.integer "track_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "tracks", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "genre"
t.integer "user_id"
t.string "name"
t.date "release_date"
end
create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "username"
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end
add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id"
end
Here is my track model
class Track < ApplicationRecord
has_many_attached :mp3
belongs_to :user, optional: true
has_many :playlist_tracks
has_many :playlists, through: :playlist_tracks
has_many :plays
end
and here is my playist_track model
class PlaylistTrack < ApplicationRecord
belongs_to :playlist
belongs_to :track
end

rails rake db:migrate sqllite3 error

StandardError: An error has occurred, this and all later migrations canceled:
SQLite3::SQLException: near "ENGINE": syntax error: CREATE TABLE "adv_bgs" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "adv_bg_type" varchar, "adv_bg_id" integer, "uploaded_file_file_name" varchar, "uploaded_file_content_type" varchar, "uploaded_file_file_size" integer, "uploaded_file_updated_at" datetime, "head" boolean DEFAULT 'f', "imageable_id" integer, "imageable_type" varchar, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL) ENGINE=MyISAM DEFAULT CHARSET=utf8
/mnt/c/Users/direwolf/Documents/web/rails_projects/okar/db/migrate/20170425144835_init_schema.rb:4:in `up'
/home/jonsdirewolf/.rbenv/versions/2.4.0/bin/bundle:22:in `load'
/home/jonsdirewolf/.rbenv/versions/2.4.0/bin/bundle:22:in `<main>'
ActiveRecord::StatementInvalid: SQLite3::SQLException: near "ENGINE": syntax error: CREATE TABLE "adv_bgs" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "adv_bg_type" varchar, "adv_bg_id" integer, "uploaded_file_file_name" varchar, "uploaded_file_content_type" varchar, "uploaded_file_file_size" integer, "uploaded_file_updated_at" datetime, "head" boolean DEFAULT 'f', "imageable_id" integer, "imageable_type" varchar, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL) ENGINE=MyISAM DEFAULT CHARSET=utf8
/mnt/c/Users/direwolf/Documents/web/rails_projects/okar/db/migrate/20170425144835_init_schema.rb:4:in `up'
/home/jonsdirewolf/.rbenv/versions/2.4.0/bin/bundle:22:in `load'
/home/jonsdirewolf/.rbenv/versions/2.4.0/bin/bundle:22:in `<main>'
SQLite3::SQLException: near "ENGINE": syntax error
/mnt/c/Users/direwolf/Documents/web/rails_projects/okar/db/migrate/20170425144835_init_schema.rb:4:in `up'
/home/jonsdirewolf/.rbenv/versions/2.4.0/bin/bundle:22:in `load'
/home/jonsdirewolf/.rbenv/versions/2.4.0/bin/bundle:22:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
What Do I do?
20170425144835_init_schema.rb
class InitSchema < ActiveRecord::Migration
def up
create_table "adv_bgs", force: :cascade, options: "ENGINE=MyISAM DEFAULT CHARSET=utf8" do |t|
t.string "adv_bg_type"
t.integer "adv_bg_id"
t.string "uploaded_file_file_name"
t.string "uploaded_file_content_type"
t.integer "uploaded_file_file_size"
t.datetime "uploaded_file_updated_at"
t.boolean "head", default: false
t.integer "imageable_id"
t.string "imageable_type"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["adv_bg_type", "adv_bg_id"], name: "index_adv_bgs_on_adv_bg_type_and_adv_bg_id", using: :btree
t.index ["head"], name: "index_adv_bgs_on_head", using: :btree
end
create_table "adv_logos", force: :cascade, options: "ENGINE=MyISAM DEFAULT CHARSET=utf8" do |t|
t.string "adv_logo_type"
t.integer "adv_logo_id"
t.string "uploaded_file_file_name"
t.string "uploaded_file_content_type"
t.integer "uploaded_file_file_size"
t.datetime "uploaded_file_updated_at"
t.boolean "head", default: false
t.integer "imageable_id"
t.string "imageable_type"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["adv_logo_type", "adv_logo_id"], name: "index_adv_logos_on_adv_logo_type_and_adv_logo_id", using: :btree
t.index ["head"], name: "index_adv_logos_on_head", using: :btree
end
create_table "adv_pic1s", force: :cascade, options: "ENGINE=MyISAM DEFAULT CHARSET=utf8" do |t|
t.string "adv_pic1_type"
t.integer "adv_pic1_id"
t.string "uploaded_file_file_name"
t.string "uploaded_file_content_type"
t.integer "uploaded_file_file_size"
t.datetime "uploaded_file_updated_at"
t.boolean "head", default: false
t.integer "imageable_id"
t.string "imageable_type"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["adv_pic1_type", "adv_pic1_id"], name: "index_adv_pic1s_on_adv_pic1_type_and_adv_pic1_id", using: :btree
t.index ["head"], name: "index_adv_pic1s_on_head", using: :btree
end
create_table "adv_pic2s", force: :cascade, options: "ENGINE=MyISAM DEFAULT CHARSET=utf8" do |t|
t.string "adv_pic2_type"
t.integer "adv_pic2_id"
t.string "uploaded_file_file_name"
t.string "uploaded_file_content_type"
t.integer "uploaded_file_file_size"
t.datetime "uploaded_file_updated_at"
t.boolean "head", default: false
t.integer "imageable_id"
t.string "imageable_type"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["adv_pic2_type", "adv_pic2_id"], name: "index_adv_pic2s_on_adv_pic2_type_and_adv_pic2_id", using: :btree
t.index ["head"], name: "index_adv_pic2s_on_head", using: :btree
end
create_table "adv_pic3s", force: :cascade, options: "ENGINE=MyISAM DEFAULT CHARSET=utf8" do |t|
t.string "adv_pic3_type"
t.integer "adv_pic3_id"
t.string "uploaded_file_file_name"
t.string "uploaded_file_content_type"
t.integer "uploaded_file_file_size"
t.datetime "uploaded_file_updated_at"
t.boolean "head", default: false
t.integer "imageable_id"
t.string "imageable_type"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["adv_pic3_type", "adv_pic3_id"], name: "index_adv_pic3s_on_adv_pic3_type_and_adv_pic3_id", using: :btree
t.index ["head"], name: "index_adv_pic3s_on_head", using: :btree
end
create_table "adv_pic4s", force: :cascade, options: "ENGINE=MyISAM DEFAULT CHARSET=utf8" do |t|
t.string "adv_pic4_type"
t.integer "adv_pic4_id"
t.string "uploaded_file_file_name"
t.string "uploaded_file_content_type"
t.integer "uploaded_file_file_size"
t.datetime "uploaded_file_updated_at"
t.boolean "head", default: false
t.integer "imageable_id"
t.string "imageable_type"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["adv_pic4_type", "adv_pic4_id"], name: "index_adv_pic4s_on_adv_pic4_type_and_adv_pic4_id", using: :btree
t.index ["head"], name: "index_adv_pic4s_on_head", using: :btree
end
create_table "adv_pic5s", force: :cascade, options: "ENGINE=MyISAM DEFAULT CHARSET=utf8" do |t|
t.string "adv_pic5_type"
t.integer "adv_pic5_id"
t.string "uploaded_file_file_name"
t.string "uploaded_file_content_type"
t.integer "uploaded_file_file_size"
t.datetime "uploaded_file_updated_at"
t.boolean "head", default: false
t.integer "imageable_id"
t.string "imageable_type"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["adv_pic5_type", "adv_pic5_id"], name: "index_adv_pic5s_on_adv_pic5_type_and_adv_pic5_id", using: :btree
t.index ["head"], name: "index_adv_pic5s_on_head", using: :btree
end
create_table "adv_pic6s", force: :cascade, options: "ENGINE=MyISAM DEFAULT CHARSET=utf8" do |t|
t.string "adv_pic6_type"
t.integer "adv_pic6_id"
t.string "uploaded_file_file_name"
t.string "uploaded_file_content_type"
t.integer "uploaded_file_file_size"
t.datetime "uploaded_file_updated_at"
t.boolean "head", default: false
t.integer "imageable_id"
t.string "imageable_type"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["adv_pic6_type", "adv_pic6_id"], name: "index_adv_pic6s_on_adv_pic6_type_and_adv_pic6_id", using: :btree
t.index ["head"], name: "index_adv_pic6s_on_head", using: :btree
end
create_table "adv_pic7s", force: :cascade, options: "ENGINE=MyISAM DEFAULT CHARSET=utf8" do |t|
t.string "adv_pic7_type"
t.integer "adv_pic7_id"
t.string "uploaded_file_file_name"
t.string "uploaded_file_content_type"
t.integer "uploaded_file_file_size"
t.datetime "uploaded_file_updated_at"
t.boolean "head", default: false
t.integer "imageable_id"
t.string "imageable_type"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["adv_pic7_type", "adv_pic7_id"], name: "index_adv_pic7s_on_adv_pic7_type_and_adv_pic7_id", using: :btree
t.index ["head"], name: "index_adv_pic7s_on_head", using: :btree
end
create_table "advertising_applications", force: :cascade, options: "ENGINE=MyISAM DEFAULT CHARSET=utf8" do |t|
t.integer "user_id"
t.integer "anon_user_id"
t.integer "televisor_id", limit: 1, null: false
t.integer "advertising_public_status_id", limit: 1, default: 1
t.integer "advertising_period_id", limit: 1, default: 1
t.integer "payment_status_id", limit: 1, default: 1
t.integer "step", limit: 1, default: 1
t.integer "spec_id", limit: 1, default: 1
t.string "email"
t.string "lnk"
t.string "redwords"
t.string "zkname"
t.string "zkdescr"
t.integer "price"
t.integer "charge", limit: 1, default: 1
t.datetime "endtime", default: '2017-04-25 17:47:04'
t.text "comment", limit: 65535
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["advertising_period_id"], name: "index_advertising_applications_on_advertising_period_id", using: :btree
t.index ["advertising_public_status_id"], name: "index_advertising_applications_on_advertising_public_status_id", using: :btree
t.index ["anon_user_id"], name: "index_advertising_applications_on_anon_user_id", using: :btree
t.index ["payment_status_id"], name: "index_advertising_applications_on_payment_status_id", using: :btree
t.index ["televisor_id"], name: "index_advertising_applications_on_televisor_id", using: :btree
t.index ["user_id"], name: "index_advertising_applications_on_user_id", using: :btree
end
create_table "advertising_periods", force: :cascade, options: "ENGINE=MyISAM DEFAULT CHARSET=utf8" do |t|
t.string "value"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "advertising_public_statuses", force: :cascade, options: "ENGINE=MyISAM DEFAULT CHARSET=utf8" do |t|
t.string "value"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "anon_saved_searches", force: :cascade, options: "ENGINE=MyISAM DEFAULT CHARSET=utf8" do |t|
t.string "subtype", limit: 3, null: false
t.string "link_to", limit: 64, null: false
t.string "name", limit: 32
t.string "category"
t.string "square_from", limit: 24
t.string "square_to", limit: 24
t.string "price_from", limit: 24
t.string "price_to", limit: 24
t.string "price_src", limit: 3
t.text "data", limit: 65535
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.text "metro_data", limit: 65535, null: false
t.integer "add_params_count", limit: 1, unsigned: true
t.integer "main_city", limit: 1, unsigned: true
t.text "location_tags_data", limit: 65535, null: false
end
create_table "anon_users", force: :cascade, options: "ENGINE=MyISAM DEFAULT CHARSET=utf8" do |t|
t.string "email"
t.string "access_level", limit: 16, default: "anon", null: false
t.string "access_code", limit: 32, null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["email"], name: "index_anon_users_on_email", using: :btree
end
create_table "area_types", force: :cascade, options: "ENGINE=MyISAM DEFAULT CHARSET=utf8" do |t|
t.string "value"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "avatars", force: :cascade, options: "ENGINE=MyISAM DEFAULT CHARSET=utf8" do |t|
t.string "imageable_type"
t.integer "imageable_id"
t.string "uploaded_file_file_name"
t.string "uploaded_file_content_type"
t.integer "uploaded_file_file_size"
t.datetime "uploaded_file_updated_at"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["imageable_type", "imageable_id"], name: "index_avatars_on_imageable_type_and_imageable_id", using: :btree
end
create_table "bad_room_kinds", force: :cascade, options: "ENGINE=MyISAM DEFAULT CHARSET=utf8" do |t|
t.string "value"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "bad_room_nums", force: :cascade, options: "ENGINE=MyISAM DEFAULT CHARSET=utf8" do |t|
t.string "value"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "bad_room_presents", force: :cascade, options: "ENGINE=MyISAM DEFAULT CHARSET=utf8" do |t|
t.string "value"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "balcons", force: :cascade, options: "ENGINE=MyISAM DEFAULT CHARSET=utf8" do |t|
t.string "value"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "bg_cn_names", force: :cascade, options: "ENGINE=MyISAM DEFAULT CHARSET=utf8" do |t|
t.string "value"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["value"], name: "ivalue", type: :fulltext
end
You are using MySQL-specific syntax ENGINE=MyISAM DEFAULT CHARSET=utf8 in SQLite database: SQLite3::SQLException: near "ENGINE":

Issue with geocoder using postgresql

I have a model called room and in the show view I want to display other rooms nearby.
show.html.erb
<div>
<% for room in #room.nearbys(10) %>
<%= image_tag room.photos[0].image.url(:medium) %>
<%= link_to room.listing_name, room %><br>
(<%= room.distance.round(2) %> miles away)
<% end %>
</div>
schema.rb
ActiveRecord::Schema.define(version: 20161006135631) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
create_table "active_admin_comments", force: :cascade do |t|
t.string "namespace"
t.text "body"
t.string "resource_id", null: false
t.string "resource_type", null: false
t.string "author_type"
t.integer "author_id"
t.datetime "created_at"
t.datetime "updated_at"
t.index ["author_type", "author_id"], name: "index_active_admin_comments_on_author_type_and_author_id", using: :btree
t.index ["namespace"], name: "index_active_admin_comments_on_namespace", using: :btree
t.index ["resource_type", "resource_id"], name: "index_active_admin_comments_on_resource_type_and_resource_id", using: :btree
end
create_table "admin_users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.inet "current_sign_in_ip"
t.inet "last_sign_in_ip"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["email"], name: "index_admin_users_on_email", unique: true, using: :btree
t.index ["reset_password_token"], name: "index_admin_users_on_reset_password_token", unique: true, using: :btree
end
create_table "photos", force: :cascade do |t|
t.integer "room_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "image_file_name"
t.string "image_content_type"
t.integer "image_file_size"
t.datetime "image_updated_at"
t.index ["room_id"], name: "index_photos_on_room_id", using: :btree
end
create_table "rooms", force: :cascade do |t|
t.string "listing_name"
t.string "accommodation_type"
t.integer "persons"
t.integer "property"
t.integer "living_area"
t.text "rooms_total"
t.text "features_short"
t.string "pets"
t.string "smoking"
t.string "check_in"
t.string "check_out"
t.string "location"
t.text "distance"
t.text "features_long"
t.text "detailed_description"
t.text "house_rules"
t.string "address"
t.text "video"
t.integer "nightly_price"
t.integer "hourly_price"
t.text "detailed_price"
t.boolean "active"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.float "latitude"
t.float "longitude"
end
create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.inet "current_sign_in_ip"
t.inet "last_sign_in_ip"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "firstname"
t.string "lastname"
t.string "provider"
t.string "uid"
t.index ["email"], name: "index_users_on_email", unique: true, using: :btree
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree
end
add_foreign_key "photos", "rooms"
end
I have used the RailsCasts Episode for orientation.
Unfortunately I am getting this Error:
How can I make this work?
Any hints for a solution are very appreciated!
Your rooms table have column distance.
And your SQL query is creating an alias distance.(AS DISTANCE).
Hence PostgreS is raising ambiguous column error.
This query is generated by gem which you are using I recommend you to change the column name of column distance to something else like my_distance or something which will not conflict with the query generated by gem.

Resources