I have a tables residence_staff and halls. In the residence_staff table there is a field position and, if it is manager, I want to display a list of all the managers and the hallName, where they work in. I can print the names of managers just fine, but the application can not recognize hallName. The tables are joined using the staffNumber as a foreign key in the halls table, to staffNumber in the residence_staff table. What am I doing wrong?
residence_staff Model:
class ResidenceStaff < ApplicationRecord
belongs_to :hall, primary_key: 'hallId'
class << self
def managers
where(position: 'manager')
end
end
end
halls Model:
class Hall < ApplicationRecord
has_many :residence_staffs
end
Controller:
class HomeController < ApplicationController
def index
#residence_staff = ResidenceStaff.all
#residence_staff_managers = ResidenceStaff.managers.includes(:hall)
end
end
View:
<h1>Title</h1>
<div>
<% #residence_staff_managers.each do |residence_staff| %>
<%= residence_staff.fName%>
<%= residence_staff.hallName%>
<% end %>
</div>
Schema.rb:
ActiveRecord::Schema.define(version: 20180426051412) do
create_table "advisor", primary_key: "advisorNumber", id: :integer, default: nil, force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
t.text "fName", null: false
t.text "lName", null: false
t.text "position", null: false
t.text "deptName", null: false
t.integer "phoneNumber", null: false
t.string "email", limit: 100, null: false
t.integer "roomNumber", null: false
t.index ["advisorNumber"], name: "Advisor_advisorNumber_uindex", unique: true
t.index ["email"], name: "Advisor_email_uindex", unique: true
end
create_table "hall_of_residences", primary_key: "hallName", id: :string, limit: 50, force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
t.string "street", limit: 50, null: false
t.text "city", null: false
t.integer "postcode", null: false
t.integer "phoneNumber", null: false
t.text "hall", null: false
t.integer "staffNumber", null: false
t.index ["hallName"], name: "HallOfResidence_hallName_uindex", unique: true
t.index ["staffNumber"], name: "HallOfResidence_ResidenceStaff_staffNumber_fk"
end
create_table "halls", primary_key: "hallId", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
t.text "hallName", null: false
t.text "street", null: false
t.text "city", null: false
t.integer "postcode", null: false
t.integer "phoneNumber", null: false
t.integer "sfaffNumber", null: false
t.index ["sfaffNumber"], name: "halls_residence_staffs_staffNumber_fk"
end
create_table "invoice", primary_key: "invoiceNumber", id: :integer, default: nil, force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
t.string "semester", limit: 50
t.date "paymentDue"
t.text "studentFName", null: false
t.text "studentLName", null: false
t.string "roomLocation", limit: 50, null: false
t.text "city", null: false
t.string "street", limit: 50, null: false
t.integer "postcode", null: false
t.integer "leaseNumber", null: false
t.index ["invoiceNumber"], name: "Invoice_invoiceNumber_uindex", unique: true
t.index ["leaseNumber"], name: "Invoice_Lease_leaseNumber_fk"
end
create_table "invoice_payment", primary_key: "paymentID", id: :integer, default: nil, force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
t.date "datePayed", null: false
t.string "method", limit: 50, null: false
t.date "dateOfFirstReminder"
t.date "dateOfSecondReminder"
t.integer "muNum", null: false
t.index ["muNum"], name: "InvoicePayment_Student_mUNumber_fk"
t.index ["paymentID"], name: "InvoicePayment_paymentID_uindex", unique: true
end
create_table "lease", id: false, force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
t.integer "leaseNumber", null: false
t.string "duration", limit: 50, null: false
t.text "studentFName", null: false
t.text "studentLName", null: false
t.integer "placeNumber", null: false
t.integer "roomNumber", null: false
t.text "city", null: false
t.string "street", limit: 50, null: false
t.integer "postcode", null: false
t.date "dateToLeave", null: false
t.date "dateToEnter", null: false
t.integer "mUNum", null: false
t.index ["leaseNumber"], name: "Lease_leaseNumber_uindex", unique: true
t.index ["mUNum"], name: "Lease_Student_mUNumber_fk"
t.index ["placeNumber"], name: "Lease_placeNumber_uindex", unique: true
end
create_table "next_of_kin", primary_key: "nextID", id: :integer, default: nil, force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
t.text "fName", null: false
t.text "lName", null: false
t.text "relationship", null: false
t.text "city", null: false
t.string "street", limit: 100, null: false
t.integer "postcode", null: false
t.integer "phoneNumber", null: false
t.integer "mUNum", null: false
t.index ["mUNum"], name: "NextOfKin_Student_mUNumber_fk"
t.index ["nextID"], name: "NextOfKin_nextID_uindex", unique: true
end
create_table "parking_lot", primary_key: "lotNumber", id: :integer, default: nil, force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
t.text "lotName", null: false
t.string "street", limit: 100, null: false
t.text "city", null: false
t.integer "postcode", null: false
t.integer "maxSpace", null: false
t.integer "availability", null: false
t.index ["lotNumber"], name: "ParkingLot_lotNumber_uindex", unique: true
end
create_table "residence_staffs", primary_key: "staffNumber", id: :integer, default: nil, force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
t.text "fName", null: false
t.text "lName", null: false
t.string "email", limit: 100, null: false
t.text "homeCity", null: false
t.string "street", limit: 50, null: false
t.integer "postal", null: false
t.date "dob", null: false
t.text "gender"
t.text "position", null: false
t.string "location", limit: 50, null: false
t.integer "phoneNumber", null: false
t.integer "inspectedRoomID", null: false
t.string "hall", limit: 50, null: false
t.index ["inspectedRoomID"], name: "ResidenceStaff_StudentApartmentInspection_inspectedRoomID_fk"
t.index ["staffNumber"], name: "ResidenceStaff_staffNumber_uindex", unique: true
end
create_table "room", primary_key: "placeNumber", id: :integer, default: nil, force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
t.string "rentRate", limit: 50, null: false
t.text "availability", null: false
t.integer "roomNumber"
t.string "hallName", limit: 50
t.integer "apartmentNumber"
t.index ["apartmentNumber"], name: "room_StudentFlats_apartmentNumber_fk"
t.index ["hallName"], name: "room_HallOfResidence_hallName_fk"
end
create_table "staff", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.text "fName"
end
create_table "student", primary_key: "mUNumber", id: :integer, default: nil, force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
t.text "fName", null: false
t.text "lName", null: false
t.string "street", limit: 100, null: false
t.text "city", null: false
t.integer "postcode", null: false
t.integer "phoneNumber", null: false
t.string "email", limit: 100, null: false
t.date "dob", null: false
t.text "gender"
t.text "standing", null: false
t.text "nationality", null: false
t.string "specialNeeds", limit: 100
t.text "major", null: false
t.string "statusOfStudent", limit: 100
t.text "minor"
t.text "comments"
t.integer "advisorNumber", null: false
t.index ["advisorNumber"], name: "Student_Advisor_advisorNumber_fk"
t.index ["email"], name: "Student_email_uindex", unique: true
t.index ["mUNumber"], name: "Student_mUNumber_uindex", unique: true
end
create_table "student_apartment_inspection", primary_key: "inspectedRoomID", id: :integer, default: nil, force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
t.text "inspectorName", null: false
t.date "dateOfInspection", null: false
t.text "apartmentCondition", null: false
t.text "comments"
t.index ["inspectedRoomID"], name: "StudentApartmentInspection_inspectedRoomID_uindex", unique: true
end
create_table "student_flats", primary_key: "apartmentNumber", id: :integer, default: nil, force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
t.integer "numberOfRooms", null: false
t.text "city", null: false
t.string "street", limit: 50, null: false
t.integer "postcode", null: false
t.integer "leaseID", null: false
t.index ["apartmentNumber"], name: "StudentFlats_apartmentNumber_uindex", unique: true
t.index ["leaseID"], name: "StudentFlats_Lease_leaseNumber_fk"
end
create_table "vehicle", primary_key: "vNumber", id: :integer, default: nil, force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
t.string "plateNumber", limit: 10, null: false
t.text "color", null: false
t.text "manufacturer", null: false
t.string "brand", limit: 50, null: false
t.integer "mUNum", null: false
t.integer "lotNumber", null: false
t.index ["lotNumber"], name: "Vehicle_ParkingLot_lotNumber_fk"
t.index ["mUNum"], name: "Vehicle_Student_mUNumber_fk"
t.index ["plateNumber"], name: "Vehicle_plateNumber_uindex", unique: true
t.index ["vNumber"], name: "Vehicle_vNumber_uindex", unique: true
end
add_foreign_key "hall_of_residences", "residence_staffs", column: "staffNumber", primary_key: "staffNumber", name: "HallOfResidence_ResidenceStaff_staffNumber_fk"
add_foreign_key "halls", "residence_staffs", column: "sfaffNumber", primary_key: "staffNumber", name: "halls_residence_staffs_staffNumber_fk"
add_foreign_key "invoice", "lease", column: "leaseNumber", primary_key: "leaseNumber", name: "Invoice_Lease_leaseNumber_fk"
add_foreign_key "invoice_payment", "student", column: "muNum", primary_key: "mUNumber", name: "InvoicePayment_Student_mUNumber_fk"
add_foreign_key "lease", "student", column: "mUNum", primary_key: "mUNumber", name: "Lease_Student_mUNumber_fk"
add_foreign_key "next_of_kin", "student", column: "mUNum", primary_key: "mUNumber", name: "NextOfKin_Student_mUNumber_fk"
add_foreign_key "residence_staffs", "student_apartment_inspection", column: "inspectedRoomID", primary_key: "inspectedRoomID", name: "ResidenceStaff_StudentApartmentInspection_inspectedRoomID_fk"
add_foreign_key "room", "hall_of_residences", column: "hallName", primary_key: "hallName", name: "room_HallOfResidence_hallName_fk"
add_foreign_key "room", "student_flats", column: "apartmentNumber", primary_key: "apartmentNumber", name: "room_StudentFlats_apartmentNumber_fk"
add_foreign_key "student", "advisor", column: "advisorNumber", primary_key: "advisorNumber", name: "Student_Advisor_advisorNumber_fk"
add_foreign_key "student_flats", "lease", column: "leaseID", primary_key: "leaseNumber", name: "StudentFlats_Lease_leaseNumber_fk"
add_foreign_key "vehicle", "parking_lot", column: "lotNumber", primary_key: "lotNumber", name: "Vehicle_ParkingLot_lotNumber_fk"
add_foreign_key "vehicle", "student", column: "mUNum", primary_key: "mUNumber", name: "Vehicle_Student_mUNumber_fk"
end
Instead of <%= residence_staff.hallName %> use <%= residence_staff.hall.hallName %> because hallName is an attribute of hall.
As an aside, ruby naming conventions favor snake case. hallName would be hall_name instead.
I'm working on a Ruby on Rails project and I get the following error when assigning a value to a foreign key:
ActiveRecord::InvalidForeignKey in InformationController#import
Mysql2::Error: Cannot add or update a child row: a foreign key
constraint fails (`sobrecupos_development`.`information`, CONSTRAINT
`fk_rails_21c7b6e26c` FOREIGN KEY (`program_id`) REFERENCES `programs`
(`id`)): UPDATE `information` SET `system_plan` = '2879.0',
`program_id` = 2879, `updated_at` = '2018-04-03 15:32:07' WHERE
`information`.`id` = 11
This key is supposed to be assigned from an external file but when I try to do so I get the error mentioned above. I tried to assign a key explicitly to check if it has to do with a possible nonexisiting value, but I still get this error.
Here is my schema.rb file
ActiveRecord::Schema.define(version: 20180402215605) do
create_table "coordinators", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
t.string "name"
t.string "lastname"
t.integer "department_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "email", default: "", null: false
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.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.string "username"
t.index ["department_id"], name: "index_coordinators_on_department_id", using: :btree
t.index ["email"], name: "index_coordinators_on_email", unique: true, using: :btree
t.index ["username"], name: "index_coordinators_on_username", unique: true, using: :btree
end
create_table "departments", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "code"
t.string "abbreviation"
t.index ["code"], name: "index_departments_on_code", unique: true, using: :btree
end
create_table "groups", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
t.integer "subject_id"
t.integer "professor_id"
t.integer "available_spots"
t.integer "total_spots"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["professor_id"], name: "index_groups_on_professor_id", using: :btree
t.index ["subject_id"], name: "index_groups_on_subject_id", using: :btree
end
create_table "information", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
t.string "level"
t.integer "program_id"
t.string "request_type"
t.string "act_resolution"
t.date "resolution_date"
t.string "dependency"
t.decimal "PAPA", precision: 10, scale: 1
t.integer "registered_credits"
t.integer "outstanding_credits"
t.decimal "completion_percentage", precision: 10
t.integer "tuition_amount"
t.string "academic_history_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "student_id"
t.boolean "was_saved"
t.string "system_plan"
t.string "academic_period"
t.string "document"
t.index ["program_id"], name: "index_information_on_program_id", using: :btree
t.index ["student_id"], name: "index_information_on_student_id", using: :btree
end
create_table "professors", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "programs", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
t.string "name"
t.integer "department_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "code"
t.string "program_type"
t.index ["code"], name: "index_programs_on_code", unique: true, using: :btree
t.index ["department_id"], name: "index_programs_on_department_id", using: :btree
end
create_table "students", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
t.string "name"
t.string "lastname"
t.string "username"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "email", default: "", null: false
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.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.string "document"
t.index ["email"], name: "index_students_on_email", unique: true, using: :btree
t.index ["username"], name: "index_students_on_username", unique: true, using: :btree
end
create_table "subject_requests", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
t.integer "group_id"
t.integer "information_id"
t.boolean "was_canceled"
t.string "approbation_state"
t.string "inscription_state"
t.string "replace_to"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.boolean "accept_other_group"
t.string "subject_code"
t.string "group_code"
t.string "typology"
t.string "subject_plan"
t.index ["group_id"], name: "index_subject_requests_on_group_id", using: :btree
t.index ["information_id"], name: "index_subject_requests_on_information_id", using: :btree
end
create_table "subjects", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
t.string "name"
t.integer "department_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "code"
t.integer "credits"
t.index ["code"], name: "index_subjects_on_code", unique: true, using: :btree
t.index ["department_id"], name: "index_subjects_on_department_id", using: :btree
end
add_foreign_key "coordinators", "departments"
add_foreign_key "groups", "professors"
add_foreign_key "groups", "subjects"
add_foreign_key "information", "programs"
add_foreign_key "information", "students"
add_foreign_key "programs", "departments"
add_foreign_key "subject_requests", "groups"
add_foreign_key "subject_requests", "information", on_delete: :cascade
add_foreign_key "subjects", "departments"
end
And here the line of code where I'm trying to assign a value to the foreign key
information.program_id = information.system_plan
Maybe there is 2 different approach you should try:
First, try cast to integer before assign:
information.program_id = information.system_plan.to_i
Second, try assign object
information.program = Program.find(information.system_plan.to_i)
Besides these 2, I could not see other wrong doings in your code.
I have a rails application which is running MySQL in production and SQLite in development. In development mode everything is working fine, but in production datetime properties just won't get saved to the database (even the created_at and updated_at won't). The field is not null, but 0000-00-00 00:00:00. How is this possible? I did some research but I can't fix it.
schema.rb:
# encoding: UTF-8
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your
# database schema. If you need to create the application database on another
# system, you should be using db:schema:load, not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20130306100623) do
create_table "announcements", :force => true do |t|
t.string "title"
t.text "text"
t.integer "user_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "delayed_jobs", :force => true do |t|
t.integer "priority", :default => 0
t.integer "attempts", :default => 0
t.text "handler"
t.text "last_error"
t.datetime "run_at"
t.datetime "locked_at"
t.datetime "failed_at"
t.string "locked_by"
t.string "queue"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
add_index "delayed_jobs", ["priority", "run_at"], :name => "delayed_jobs_priority"
create_table "document_categories", :force => true do |t|
t.string "title"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "documents", :force => true do |t|
t.string "file_file_name"
t.string "file_content_type"
t.integer "file_file_size"
t.datetime "file_updated_at"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.integer "user_id"
t.string "title"
t.text "description"
t.string "ancestry"
t.boolean "folder"
end
add_index "documents", ["ancestry"], :name => "index_documents_on_ancestry"
create_table "inventory_items", :force => true do |t|
t.string "object"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "reservation_items", :force => true do |t|
t.integer "reservation_id"
t.integer "inventory_item_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "reservations", :force => true do |t|
t.integer "user_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "start_time"
t.datetime "end_time"
end
create_table "users", :force => true do |t|
t.string "first_name"
t.string "insertion"
t.string "last_name"
t.string "telephone_prefix"
t.string "telephone_suffix"
t.string "address"
t.string "number"
t.string "postal_code"
t.string "city"
t.date "birthdate"
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
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "telephone_mobile"
t.integer "admin"
t.string "avatar_file_name"
t.string "avatar_content_type"
t.integer "avatar_file_size"
t.datetime "avatar_updated_at"
t.string "sex"
t.string "lovo_email"
end
add_index "users", ["email"], :name => "index_users_on_email", :unique => true
add_index "users", ["reset_password_token"], :name => "index_users_on_reset_password_token", :unique => true
end
Thank a lot!