我正在使用舊版數據庫,並且還通過Rails guides on associations進行了讀取。與Rails 3中的遺留數據庫的簡單關聯?
我有兩種模式。 A diary
模型和animal
模型。
diary.rb
class Diary < ActiveRecord::Base
establish_connection :premvet
attr_accessible :DiaryNo ,:DiaryName, :DiaryDate, :SlotHour, :TotalSlots, :BlockSlots, :BlockBooked, :FreeSlot, :BookedSlot
self.table_name = 'diary'
self.primary_key = 'DiaryNo'
has_many :animals, :foreign_key => 'DiaryQueue'
end
animal.rb
class Animal < ActiveRecord::Base
establish_connection :premvet
self.table_name = 'animal'
self.primary_key = 'PVID'
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'
belongs_to :diary, :foreign_key => 'DiaryNo'
end
動物索引視圖
<% @animals_todaysappointments.each do |animal| %>
<tr>
<td><%= animal.id %></td>
<td><%= animal.AnimalName %></td>
<td><%= link_to animal.client.Surname, animal.client %></td>
<td><%= animal.Species %></td>
<td><%= animal.Breed %></td>
<td><%= animal.NextApptDate.strftime("%d %b. %Y - %H:%M") %></td>
<td><%= animal.DiaryQueue %>
<td><%= animal.diary.DiaryName %></td>
<td><%= link_to 'Show', animal %></td>
</tr>
<% end %>
,你可以SE E - 這是使用顯示DiaryQueue從動物表:
<td><%= animal.DiaryQueue %>
,我試圖從與日記表顯示DiaryName:
<td><%= animal.diary.DiaryName %></td>
這失敗,異常錯誤:
undefined method `DiaryName' for nil:NilClass
@animals_todaysappointments
方法如下所示:
def appointments
@animals_todaysappointments = Animal.where('DATE(NextApptDate) = ?', Date.today).page(params[:page]).per_page(15).order('NextApptDate ASC')
respond_to do |format|
format.html # index.html.erb
end
end
如果我添加:
delegate :DiaryName, :to => :diary
的動物模型,然後我得到以下錯誤:
Animal#DiaryName delegated to diary.DiaryName, but diary is nil: #
每個動物記錄了0和10日記表之間的DiaryQueue值有許多行中,例行:
DiaryNo DiaryName DiaryDate SlotHour TotalSlots BlockBooked FreeSlot BookedSlot
--------------------------------------------------------------------------------------
1 Morning 2012-07-16 9 18 0 0 18
有與同DiaryNo行,但他們也有同樣的DiaryName。它只是在這些行中改變的DiaryDate,SlotHour,TotalSlots,BlockBooked,FreeSlot和BookedSlot。
要儘量防止我加了錯誤以下的觀點:
<% unless animal.DiaryName.nil? %>
<td><%= animal.DiaryName %></td>
<% else %>
<td><%= animal.DiaryQueue %>
<% end %>
,但我得到一個異常:
Animal#DiaryName delegated to diary.DiaryName, but diary is nil: #
我在做什麼錯?
你說它失敗,發生異常錯誤。你可以發佈引發的全部異常嗎? – jstr 2012-08-09 11:03:40
對不起,只是在我的問題中加入了它。 – dannymcc 2012-08-09 11:04:22
它看起來好像沒有爲動物記錄找到關聯的日記(乳業?)記錄。你確定唱片在那? – jstr 2012-08-09 11:16:24