Skip to content

Instantly share code, notes, and snippets.

@dannymcc
Created April 24, 2012 20:46
Show Gist options
  • Select an option

  • Save dannymcc/2483578 to your computer and use it in GitHub Desktop.

Select an option

Save dannymcc/2483578 to your computer and use it in GitHub Desktop.
I'm having some trouble working with a legacy database. I've read through a few tutorials and have started work by simply listing everything in the database with the correct column names etc. It's going well but now I've come to try and associate the models with each other.
I'm getting the following error:
Mysql2::Error: Unknown column 'animal.client_id' in 'where clause': SELECT `animal`.* FROM `animal` WHERE `animal`.`client_id` = 1
The models are as follows:
**client.rb**
class Client < ActiveRecord::Base
set_table_name :client
set_primary_key :ClientKey
attr_accessible :ActiveAnimals, :AddressLine1, :AddressLine2, :AddressLine3, :BadDebt, :CID, :ClientBalance, :ClientKey, :EMail, :Fax_Number, :FirstRegistered, :Initials, :IsClient, :LastContribution, :LastPaid, :LastVisit, :MTag, :Marketing, :MasterRec, :Mobile_Number, :NumberOfAnimals, :Postcode, :SL12M, :SiteRegistered, :SpendY, :SpendY1, :SpendY2, :SpendY3, :SpendY4, :SpendY5, :Surname, :Telephone, :Telephone_2, :Telephone_3, :Telephone_4, :Telephone_5, :Telephone_6, :Telephone_7, :Telephone_8, :Telephone_9, :Title, :VL12M, :VisitY, :VisitY1, :VisitY2, :VisitY3, :VisitY4, :VisitY5
has_many :animals, :foreign_key => 'ClientKey'
end
**animal.rb**
class Animal < ActiveRecord::Base
set_table_name :animal
set_primary_key :PVID
alias_attribute :client_id, :ClientKey
attr_accessible :AddedBy, :Age, :AnimalBFAmount, :AnimalBalance, :AnimalName, :Archive, :BillType, :Breed, :ChronicStatus, :Class, :Classification, :ClientKey, :Colour, :Date1, :DateOfBirth, :DateofBirth, :Dead, :DiaryQueue, :DiscField, :DrugsAtCost, :DrugsNoVAT, :ESDAmount, :ESDType, :FNote, :FirstRegisteredDate, :Height, :IDNumber, :Insured, :InsuredWith, :IsClient, :IsClientDate, :IsMaster, :LastBilledAmount, :LastBilledDate, :LastConsDate, :LastContributionDate, :LastPaidDate, :LastWeightDate, :Locked, :LoyaltyMultiplier, :LoyaltyPoints, :MR_Flag_0, :MR_Flag_1, :MR_Flag_10, :MR_Flag_11, :MR_Flag_12, :MR_Flag_13, :MR_Flag_14, :MR_Flag_15, :MR_Flag_2, :MR_Flag_3, :MR_Flag_4, :MR_Flag_5, :MR_Flag_6, :MR_Flag_7, :MR_Flag_7, :MR_Flag_8, :MR_Flag_9, :Mileage, :Neutered, :NextApptDate, :ORT, :OldSex, :Opt_Flag_0, :Opt_Flag_1, :Opt_Flag_2, :Opt_Flag_3, :Opt_Flag_4, :Opt_Flag_5, :Opt_Flag_6, :Opt_Flag_7, :PVID, :PreferredContact, :PreferredUser, :Ref1, :RefPrac, :ReferredBy, :SSDType, :SeenInPeriod, :SendBill, :Sex, :SiteAnimal, :Species, :Status, :SurcAmount, :SurcType, :SurgeryNumber, :TBU, :TOSAmount, :TOSDrugs, :TOSFees, :TOSType, :Weight
belongs_to :client, :foreign_key => 'ClientKey'
has_many :clinicals
has_many :labs
end
I believe the problem is due to the association looking for a client_id column in the animal table. There isn't one, so I've tried adding `alias_attribute` to map the client_id missing column with the real/existing column called `ClientKey`.
I'm basically trying to list all animals that a client owns.
The show view of the client record has the following code:
<h2>Animals</h2>
<% @client.animals.each do |comment| %>
<p>
<%= animal.name %>
</p>
<% end %>
I'm trying to figure out where I'm going wrong. Any help would be appreciated!
For reference for anyone else, the source of most of my code is from http://jonathanhui.com/ruby-rails-3-model-working-legacy-database
**UPDATE**
I've realised that I've missed the :foreign_key option which I've now added. I'm still getting an error though:
undefined method `animal' for #<Client:0x007fe2839ce948>
Which is referring to the following code (line 246):
243: <h2>Animals</h2>
244: <% @client.animals.each do |comment| %>
245: <p>
246: <%= @client.animal.name %>
247: </p>
248: <% end %>
249:
I've updated the models code above.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment