Reputation: 6459
I have show.html.erb
:
<h3>Rooms (<%= @building.rooms.count %>)</h3>
<%= render @rooms %>
I'm getting this error:
OCIError: ORA-00904: "ROOMS"."BUILDING_ID": invalid identifier: \
SELECT "ROOMS".* FROM "ROOMS" WHERE "ROOMS"."BUILDING_ID" = :a1`
Here are models:
class Building < ActiveRecord::Base
has_many :rooms
end
class Room < ActiveRecord::Base
belongs_to :building
end
Here's buildings_controller.rb
:
class BuildingsController < ApplicationController
def show
@building = Building.find(params[:id])
@rooms = @building.rooms.all
end
The building
model has attributes:
ID, NAME, CREATED_AT, UPDATED_AT, CODE
The room
model has attributes:
ID, NAME, CREATED_AT, UPDATED_AT, BUILDING_CODE, NUMBER
Upvotes: 1
Views: 67
Reputation: 8295
you should define an explicit foreign_key on Room
class Room < ActiveRecord::Base
belongs_to :building, foreign_key: :code
end
that way you tell active record to use building.code for the lookup.
Of course you will also need the proper migration to define room.building_code
eg
def change
add_column :rooms, :building_code, :integer # or :string
add_index :rooms, :building_code
end
Upvotes: 1
Reputation: 9523
In your Room
model, change the association to become like this:
class Room < ActiveRecord::Base
belongs_to :building, primary_key: 'code'
end
This will tell rails that the association's primary key is code
instead of the default id
.
Upvotes: 0