在Rails 3中与遗留数据库的简单关联?

时间:2012-08-09 10:59:34

标签: ruby-on-rails-3 model associations model-associations legacy-database

我正在使用遗留数据库,并且还在阅读Rails guides on associations

我有两个型号。 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 %>

正如您所看到的那样,它使用以下方式显示动物表中的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: #

每个动物记录的DiaryQueue值介于0到10之间,日记表有多行,示例行为:

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: #

我做错了什么?

1 个答案:

答案 0 :(得分:1)

鉴于上述答案,您可能需要将Animal中的belongs_to方法更改为以下内容:

belongs_to :diary, :foreign_key => "DiaryQueue", :primary_key => "DiaryNo"

在您当前的实施中,您的模型将尝试将Animal#DiaryNoDiary#DiaryNo匹配,当您说出自己想要的是将Animal#DiaryQueueDiary#DiaryNo匹配时。