I'm sure it something really stupid, but I cannot seem to find the issue.
I'm trying to call Reservation.last.card, but get the error
Reservation Load (0.3ms) SELECT "reservations".* FROM "reservations" ORDER BY "reservations"."id" DESC LIMIT $1 [["LIMIT", 1]]
NoMethodError: undefined method `card' for #<Reservation:0x090d440e130>
Did you mean? card_id
migration + schema
class AddCardToReservations < ActiveRecord::Migration[5.2]
def change
add_reference :reservations, :card, foreign_key: true
end
end
create_table "reservations", force: :cascade do |t|
t.bigint "park_id"
t.bigint "card_id"
t.index ["card_id"], name: "index_reservations_on_card_id"
t.index ["park_id"], name: "index_reservations_on_park_id"
end
models
class Reservation < ApplicationRecord
has_one :card
belongs_to :park
end
class Card < ApplicationRecord
belongs_to :park
has_many :reservations
end
the line in the Reservation class...
has_one :card
Implies that the card object has a reservation_id which isn't the case, the foreign key is card_id in the reservation object, so what you want is...
belongs_to :card
I've been working on implementing a new model that belongs to one of our web apps existing models. Eventually, I want to build out a nested form. I understand that the form, and strong params can have it's own suite of issues, but I am currently struggling to get the models to behave as I would expect in the rails console.
Rails 4.2.7, Postgres DB
UPDATE - 10/3/16 - Still trying to find the right solution, but have made some changes
Our work is with Schools and Districts, and this particular case deals with surveys and how a survey is assigned to a school and district. Until now, a survey has been assigned to a district with a SurveyAssignment model, and some down the line logic assumed that all schools in a district were also "assigned" to the survey. Now, we want to be able to add more granularity to the SurveyAssignment and allow some specificity at the school level.
So I created a SchoolSurveyAssignment model and started to get the bits in place.
Here is the relevant model info:
class District < ActiveRecord::Base
...
has_many :schools, dependent: :destroy
has_many :survey_assignments, dependent: :destroy
...
end
class School
...
belongs_to :district
has_many :school_survey_assignments
has_many :survey_assignments, :through => :school_survey_assignments
...
end
class SurveyAssignment
belongs_to :district
belongs_to :survey
has_one :survey_version, through: :survey
has_many :school_survey_assignments, inverse_of: survey_assignment
has_many :schools, :through => :school_survey_assignments
accepts_nested_attributes_for :school_survey_assignments
attr_accessor :survey_group, :survey_version_type, :survey_version_id, :school_survey_assignments_attributes
validates :survey_id, presence: true
end
class SchoolSurveyAssignment
belongs_to :survey_assignment, inverse_of: :school_survey_assignments
belongs_to :school
attr_accessor :school_id, :survey_assignment_id, :grades_affected, :ulc_affected
validates_presence_of :survey_assignment
validates :school_id, presence: true, uniqueness: {scope: :survey_assignment_id}
end
Relevant Controller code:
class SurveyAssignmentsController < ApplicationController
before_action :set_district
before_action :set_survey_assignment, only: [:show, :edit, :update, :destroy]
respond_to :html, :json, :js
def new
#new_survey_assignment = SurveyAssignment.new()
#district.schools.each do |school|
#new_survey_assignment.school_survey_assignments.build(school_id: school.id)
end
end
def create
#survey_assignment = SurveyAssignment.new(survey_assignment_params)
if #survey_assignment.save
flash[:notice] = "Survey successfully assigned to #{#district.name}"
else
flash[:alert] = "There was a problem assigning this survey to #{#district.name}"
end
redirect_to district_survey_assignments_path(#district)
end
def survey_assignment_params
params.require(:survey_assignment).permit(:survey_id, :status, :survey_version_id, school_survey_assignments_attributes: [:id, :survey_assignment_id, :school_id, grades_affected: [], ulc_affected: []]).tap do |p|
p[:district_id] = #district.id
p[:school_year] = session[:selected_year]
end
end
def set_district
#district = District.find(params[:district_id])
end
Here is the relevant schema info:
create_table "school_survey_assignments", force: :cascade do |t|
t.integer "survey_assignment_id"
t.integer "school_id"
t.integer "grades_affected", default: [], array: true
t.string "ulc_affected", default: [], array: true
end
add_index "school_survey_assignments", ["school_id"], name: "index_school_survey_assignments_on_school_id", using: :btree
add_index "school_survey_assignments", ["survey_assignment_id"], name: "index_school_survey_assignments_on_survey_assignment_id", using: :btree
create_table "survey_assignments", force: :cascade do |t|
t.integer "district_id"
t.integer "survey_id"
t.integer "status"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "school_year"
t.integer "last_response_status_id"
end
add_index "survey_assignments", ["district_id"], name: "index_survey_assignments_on_district_id", using: :btree
Once these were in place, I stepped into my rails console and attempted the following:
2.3.1 :002 > sa1 = SurveyAssignment.create(district_id: 3, survey_id: 508, school_year: 2017)
(0.2ms) BEGIN
SQL (0.7ms) INSERT INTO "survey_assignments" ("district_id", "survey_id", "school_year", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id" [["district_id", 3], ["survey_id", 508], ["school_year", 2017], ["created_at", "2016-09-30 21:30:20.205144"], ["updated_at", "2016-09-30 21:30:20.205144"]]
(7.2ms) COMMIT
=> #<SurveyAssignment id: 369, district_id: 3, survey_id: 508, status: nil, created_at: "2016-09-30 21:30:20", updated_at: "2016-09-30 21:30:20", school_year: 2017, last_response_status_id: nil>
2.3.1 :003 > sa2 = SurveyAssignment.create(district_id: 3, survey_id: 508, school_year: 2017)
(0.3ms) BEGIN
SQL (0.4ms) INSERT INTO "survey_assignments" ("district_id", "survey_id", "school_year", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id" [["district_id", 3], ["survey_id", 508], ["school_year", 2017], ["created_at", "2016-09-30 21:30:30.701197"], ["updated_at", "2016-09-30 21:30:30.701197"]]
(0.5ms) COMMIT
=> #<SurveyAssignment id: 370, district_id: 3, survey_id: 508, status: nil, created_at: "2016-09-30 21:30:30", updated_at: "2016-09-30 21:30:30", school_year: 2017, last_response_status_id: nil>
So now, I've successfully created two Survey Assignments. I'm now going to create two School Survey Assignments off of sa1:
2.3.1 :004 > [{school_id: 5}, {school_id: 6}].each do |ssa|
2.3.1 :005 > sa1.school_survey_assignments.create(ssa)
2.3.1 :006?> end
(0.2ms) BEGIN
SchoolSurveyAssignment Exists (2.4ms) SELECT 1 AS one FROM "school_survey_assignments" WHERE ("school_survey_assignments"."school_id" = 5 AND "school_survey_assignments"."survey_assignment_id" = 369) LIMIT 1
SQL (0.4ms) INSERT INTO "school_survey_assignments" ("survey_assignment_id") VALUES ($1) RETURNING "id" [["survey_assignment_id", 369]]
(6.4ms) COMMIT
(0.6ms) BEGIN
SchoolSurveyAssignment Exists (0.4ms) SELECT 1 AS one FROM "school_survey_assignments" WHERE ("school_survey_assignments"."school_id" = 6 AND "school_survey_assignments"."survey_assignment_id" = 369) LIMIT 1
SQL (0.3ms) INSERT INTO "school_survey_assignments" ("survey_assignment_id") VALUES ($1) RETURNING "id" [["survey_assignment_id", 369]]
(0.4ms) COMMIT
=> [{:school_id=>5}, {:school_id=>6}]
2.3.1 :007 > sa1.save
(0.3ms) BEGIN
(0.4ms) COMMIT
=> true
Now, it looks like I've successfully created two SchoolSurveyAssignments with survey_assignment_id = 369 and school_ids = 5 and 6
2.3.1 :008 > sa1.school_survey_assignments
SchoolSurveyAssignment Load (0.3ms) SELECT "school_survey_assignments".* FROM "school_survey_assignments" WHERE "school_survey_assignments"."survey_assignment_id" = $1 [["survey_assignment_id", 369]]
=> #<ActiveRecord::Associations::CollectionProxy [#<SchoolSurveyAssignment id: 5, survey_assignment_id: 369, school_id: nil, grades_affected: [], ulc_affected: []>, #<SchoolSurveyAssignment id: 6, survey_assignment_id: 369, school_id: nil, grades_affected: [], ulc_affected: []>]>
As you can see from the ActivRecord::Associations::CollectionProxy, both of the SchoolSurveyAssignments were created, with survey_assignment_id: 369, but with a nil school_id. This is troubling as it seems to be
Ignoring the parameters being passed into the create function, and
ignoring the validation of school_id
Another item that I don't understand is the following:
2.3.1 :009 > SchoolSurveyAssignment.find(5).survey_assignment_id
SchoolSurveyAssignment Load (0.6ms) SELECT "school_survey_assignments".* FROM "school_survey_assignments" WHERE "school_survey_assignments"."id" = $1 LIMIT 1 [["id", 5]]
=> nil
2.3.1 :011 > SchoolSurveyAssignment.find(5).survey_assignment.id
SchoolSurveyAssignment Load (0.3ms) SELECT "school_survey_assignments".* FROM "school_survey_assignments" WHERE "school_survey_assignments"."id" = $1 LIMIT 1 [["id", 5]]
SurveyAssignment Load (0.4ms) SELECT "survey_assignments".* FROM "survey_assignments" WHERE "survey_assignments"."id" = $1 LIMIT 1 [["id", 369]]
=> 369
Calling .survey_assignment_id should return the attribute on the SchoolSurveyAssignment and give 369. .survey_assignment.id is simply just grabbing the parent object's ID. I would expect both to return the same value, but one returns nil.
The end use case is making a SurveyAssignment form that lets the user set the attributes for a new SurveyAssignment and also set the attributes for X number of SchoolSurveyAssignments (based on # of schools in a district; varies from 2 to 15). Once I get a better grasp on how these models are interacting, I feel confident in executing this goal, but the behavior I'm seeing doesn't make sense to me, and I was hoping to find some clarity on implementing these related models. I feel like I'm bouncing around the answer, but am missing a key detail.
Thanks,
Alex
Try removing your attr_accessor lines of code. attr_accessor shouldn't be used for attributes that are persisted in the database and it's probably messing up the methods that ActiveRecord already provides by default, causing those attributes to not be saved properly
class SurveyAssignment
belongs_to :district
belongs_to :survey
has_one :survey_version, through: :survey
has_many :school_survey_assignments, inverse_of: survey_assignment
has_many :schools, :through => :school_survey_assignments
accepts_nested_attributes_for :school_survey_assignments
validates :survey_id, presence: true
end
class SchoolSurveyAssignment
belongs_to :survey_assignment, inverse_of: :school_survey_assignments
belongs_to :school
validates_presence_of :survey_assignment
validates :school_id, presence: true, uniqueness: {scope: :survey_assignment_id}
end
For the first question, School and SurveyAssignment don't know each other, school_id becomes nil. In your app, these models have m-to-n association indirectly, so how about using has_many through association between models?
In School model, add below:
has_many :survey_assignments, :through => :school_survey_assignments
In SurveyAssignments model, add below:
has_many :schools, :through => :school_survey_assignments
For the last question, both codes seem to be same..
I have the followings tables
break_points
id: integer
break_point_name: string
schedules
id: integer
departure: int -> break_point_id
arrival:int -> break_point_id
departure_date:date
arrival_date: date
Both departure and arrival are breakpoints .
Then Will I to create an association * to * instead?
1.9.3-p547 :004 > s=Schedule.find(1)
Schedule Load (0.1ms) SELECT "schedules".* FROM "schedules" WHERE "schedules"."id" = ? LIMIT 1 [["id", 1]]
=> #<Schedule id: 1, departure_id: 1, departure_date: "2015-01-05", departure_time: 28800, arrival_id: 11, arrival_date: "2015-01-06", arrival_departure: 3600, bus_company_id: 1, created_at: "2014-11-08 22:55:00", updated_at: "2014-11-08 22:55:00">
1.9.3-p547 :005 > s.departure_break_points
BreakPoint Load (0.3ms) SELECT "break_points".* FROM "break_points" WHERE "break_points"."departure_id" = 1
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: break_points.departure_id: SELECT "break_points".* FROM "break_points" WHERE "break_points"."departure_id" = 1
from /home/fernando/.rvm/gems/ruby-1.9.3-p547#ticket_master/gems/sqlite3-1.3.10/lib/sqlite3/database.rb:91:in `initialize'
from /home/fernando/.rvm/gems/ruby-1.9.3-p547#ticket_master/gems/sqlite3-1.3.10/lib/sqlite3/database.rb:91:in `new'
from /home/fernando/.rvm/gems/ruby-1.9.3-p547#ticket_master/gems/sqlite3-1.3.10/lib/sqlite3/database.rb:91:in `prepare'
these are my migrations that generate the tables of the database
class CreateSchedules < ActiveRecord::Migration
def change
create_table :schedules do |t|
t.integer :departure_id
t.date :departure_date
t.time :departure_time
t.integer :arrival_id
t.date :arrival_date
t.time :arrival_departure
t.integer :bus_company_id
t.timestamps
end
end
end
class CreateBreakPoints < ActiveRecord::Migration
def change
create_table :break_points do |t|
t.string :city
t.integer :province_id
t.timestamps
end
end
end
and these are my models
These are my models
class BreakPoint < ActiveRecord::Base
attr_accessible :break_point_name, :city
belongs_to :province
end
class Schedule < ActiveRecord::Base
has_many :departure_break_points,class_name: "BreakPoint", :foreign_key => 'departure_id', :dependent => :destroy
has_many :arrival_break_points, class_name: "BreakPoint", :foreign_key => 'arrival_id', :dependent => :destroy
end
Add two relations in your model , but specify the foreign key
has_many :departure_break_points,class_name: "BreakPoint", :foreign_key => 'departure' :dependent => :destroy
has_many :arrival_break_points, class_name: "BreakPoint", :foreign_key => 'arrival', :dependent => :destroy
I'm new to Rails and I'm having an issue with models using "different" primary/foreign key naming conventions than supported by Rails. (OK, I think this MIGHT be the problem)
So these are my 2 models:
class Project < ActiveRecord::Base
self.primary_key = "PROJECT_ID"
has_many :employees, :foreign_key => "PROJECT_ID"
end
class Employee < ActiveRecord::Base
self.primary_key = "EMPLOYEE_ID"
belongs_to :project, :primary_key => "PROJECT_ID"
end
And this is what's driving me nuts:
> p = Project.find(2)
Project Load (0.2ms) SELECT "projects".* FROM "projects" WHERE "projects"."PROJECT_ID" = ? LIMIT 1 [[nil, 2]]
=> #<Project project_id: 2, name: "Project 2", created_at: "2013-08-18 21:26:33.538007", updated_at: "2013-08-18 21:26:33.538007">
> p.employees.inspect
Employee Load (0.2ms) SELECT "employees".* FROM "employees" WHERE "employees"."PROJECT_ID" = ? **[[nil, nil]]**
=> "#<ActiveRecord::Associations::CollectionProxy []>"
For some reason I don't receive the employees with project_id = 2. It seems that the ? gets substituted with nil.
It works the other way round, check this out
> e = Employee.find_by_project_id(2)
Employee Load (0.2ms) SELECT "employees".* FROM "employees" WHERE "employees"."project_id" = 2 LIMIT 1
=> #<Employee employee_id: 2, first_name: "Will", last_name: "Smith", project_id: 2, created_at: "2013-08-18 21:21:47.884919", updated_at: "2013-08-18 21:22:48.263970">
> e.project.inspect
Project Load (0.2ms) SELECT "projects".* FROM "projects" WHERE "projects"."PROJECT_ID" = ? ORDER BY "projects"."PROJECT_ID" ASC LIMIT 1 [[nil, 2]]
=> "#<Project project_id: 2, name: \"Project 2\", created_at: \"2013-08-18 21:26:33.538007\", updated_at: \"2013-08-18 21:26:33.538007\">"
What am I missing?
Try this:
class Project < ActiveRecord::Base
self.primary_key = "PROJECT_ID"
has_many :employees
end
class Employee < ActiveRecord::Base
attr_accessible :project
self.primary_key = "EMPLOYEE_ID"
belongs_to :project
end
Try to avoid upper case column names at all cost.
for the record here my schema.rb
create_table "employees", :primary_key => "EMPLOYEE_ID", :force => true do |t|
t.integer "project_id"
end
create_table "projects", :primary_key => "PROJECT_ID", :force => true do |t|
end
Try the following:
class Project < ActiveRecord::Base
self.primary_key = "PROJECT_ID"
has_many :employees, :foreign_key => "PROJECT_ID", :primary_key => "PROJECT_ID"
end
class Employee < ActiveRecord::Base
self.primary_key = "EMPLOYEE_ID"
belongs_to :project, :primary_key => "PROJECT_ID", :foreign_key => "PROJECT_ID"
end
If you use uppercased field names (i am not sure based on your question) then make sure you always use uppercased names (e.g. find_by_PROJECT_ID). Rails and ActiveRecord are case sensitive.
I have 3 tables and 3 models:
Car.rb
has_many :cars_domains, :dependent => :delete_all
has_many :domains, :through => :cars_domains
Domain.rb
has_many :cars_domains
has_many :cars, :through => :cars_domains
and cars_domain.rb
class CarsDomain < ActiveRecord::Base
belongs_to :car
belongs_to :domain
attr_accessible :car_id, :domain_id
end
class CreateCarsDomains < ActiveRecord::Migration
def change
create_table :cars_domains, :id => false do |t|
t.references :car, :domain
t.timestamps
end
add_index :cars_domains, [:car_id, :domain_id]
end
end
One car can be in some domains.
When I create a car and tied to a domain, then all is well.
But when I try destroy from cars_domains table, I have errors:
CarsDomain.where(:car_id => 2, :domain_id => 1).destroy
ArgumentError: wrong number of arguments (0 for 1)
Or
CarsDomain.where(:car_id => 2, :domain_id => 1).destroy_all
CarsDomain Load (0.5ms) SELECT "cars_domains".* FROM "cars_domains" WHERE "cars_domains"."car_id" = 2 AND "cars_do
mains"."domain_id" = 1
(0.2ms) SAVEPOINT active_record_1
Could not log "sql.active_record" event. NoMethodError: undefined method `name' for nil:NilClass
(0.3ms) ROLLBACK TO SAVEPOINT active_record_1
ActiveRecord::StatementInvalid: PGError: ERROR: zero-length delimited identifier at or near """"
LINE 1: DELETE FROM "cars_domains" WHERE "cars_domains"."" = $1
^
: DELETE FROM "cars_domains" WHERE "cars_domains"."" = $1
from /usr/local/lib/ruby/gems/1.9.1/gems/activerecord-3.1.3/lib/active_record/connection_adapters/postgresql_
adapter.rb:1062:in `prepare'
What's wrong?
UPD:
CarsDomain.destroy_all(:car_id => 2, :domain_id => 1)
CarsDomain Load (0.9ms) SELECT "cars_domains".* FROM "cars_domains" WHERE "cars_domains"."car_id" = 2 AND "cars_do
mains"."domain_id" = 1
(0.2ms) SAVEPOINT active_record_1
Could not log "sql.active_record" event. NoMethodError: undefined method `name' for nil:NilClass
(0.2ms) ROLLBACK TO SAVEPOINT active_record_1
ActiveRecord::StatementInvalid: PGError: ERROR: zero-length delimited identifier at or near """"
LINE 1: DELETE FROM "cars_domains" WHERE "cars_domains"."" = $1
^
: DELETE FROM "cars_domains" WHERE "cars_domains"."" = $1
UPD 2
I think that problem in ID ---- create_table :cars_domains, :id => false. Because Destroy method need it.
Because you got more entries, and it belongs to CarsDomain. CarsDomain#destroy expects an id, so that's why you got this message. Use destory_all with condition!
CarsDomain.destroy_all(:car_id => 2, :domain_id => 1)
You're working on a collection, so use destroy_all.
See doc.
In your Car model (car.rb) you have the dependent set to :delete_all, when it should be :destroy, meaning has_many :cars_domains, :dependent => :destroy
If your domain is also dependent you need to add :dependent => :destroy to your domain model as well.
Problem was in :
create_table :cars_domains, :id => false do |t|
Correct variant is:
create_table :cars_domains do |t|
And CarsDomain.where(:car_id => params[:car_id], :domain_id => params[:domain_id]).destroy_all