Rails 4 oracle增强适配器多对多问题

时间:2013-10-24 07:39:46

标签: ruby-on-rails oracle oracle11g ruby-on-rails-4

首先要做的事情

使用:

  • rails4
  • oracle增强适配器rails4分支

我在现有数据库上映射了多对多的关系。 我的模型看起来像这样:

class EventMap < ActiveRecord::Base
  self.table_name="TAKE_PART"
  self.primary_key="id"
  belongs_to :event, foreign_key: "lottery_event_id"  
  belongs_to :entrant, foreign_key: "address_id"  
end

class Event < ActiveRecord::Base
  self.table_name="THE_EVENT"
  self.primary_key="id"
  has_many :event_maps
  has_many :entrants, :through => :event_maps
end

class Entrant < ActiveRecord::Base
  self.table_name="ADDRESSES"
  self.primary_key="id"
  self.set_date_columns :date_of_birth
  has_many :event_maps
  has_many :events, :through => :event_maps
end

当我尝试获取事件的所有地址时:

  def show
    @entrants = Event.find(params[:id]).entrants
  end

我这样得到一个Oracle错误:

OCIError: ORA-00904: "TAKE_PART"."ENTRANT_ID": SELECT "THE_EVENT".* FROM "THE_EVENT" INNER JOIN "TAKE_PART" ON "THE_EVENT"."ID" = "TAKE_PART"."LOTTERY_EVENT_ID" WHERE "TAKE_PART"."ENTRANT_ID" = :a1

这里的问题似乎是TAKE_PART的外键没有正确使用,它应该是 address_id ,而是使用型号名称 entrant_id

这是oracle中增强的错误还是我在表的匹配方面做错了什么?

1 个答案:

答案 0 :(得分:1)

您的课程的关联未完全定义,但是EventEntrant都不知道正确的ID,他们应该转到EventMap。正确的方法如下:

class EventMap < ActiveRecord::Base
  ...
  belongs_to :event, :foreign_key => "lottery_event_id"  
  belongs_to :entrant, :foreign_key => "address_id"
end

class Event < ActiveRecord::Base
  ...
  has_many :event_maps, :foreign_key => "lottery_event_id"
  has_many :entrants, :through => :event_maps
end

class Entrant < ActiveRecord::Base
  ...
  has_many :event_maps, :foreign_key  => "address_id"
  has_many :events, :through => :event_maps
end

它有效,因为event.entrants意味着对数据库执行以下操作(简化):

  

加入两个表EventMapEntrant,以便address_id&lt; =&gt; entrant.id (映射1)
  在连接表中,使用lottery_event_id = event.id (映射2)找到所有字符串

映射1

has_many :entrants, :through => :event_maps         #Join two tables
belongs_to :entrant, :foreign_key => "address_id"   #matching address_id with entrants.id
#belongs_to+foreign_key tells to EventMap which key to use for referring to Event object

映射2

has_many :event_maps, :foreign_key => "lottery_event_id" #associate event.id with lottery_event_id
#has_many+foreign_key tells to Event which key EventMap uses for referring to Event object

或者以更正式的方式:

SELECT "entrants".* FROM "entrants" INNER JOIN "event_maps" ON "entrants"."id" =
"event_maps"."address_id" WHERE "event_maps"."lottery_event_id" = ?  [["lottery_event_id", 1]]