Has_and_belongs_to_many和单表继承

时间:2016-03-30 23:48:31

标签: ruby-on-rails rails-activerecord jointable single-table-inheritance

我需要一些帮助。我觉得非常愚蠢地尝试让它发挥作用,但实际上我无法理解这一点。

我需要在子模型和另一个模型之间建立连接表。

1)我有一个用户模型,可以按照我的权限方案进行子类化:

class User < ActiveRecord::Base

  # so we can use Single Table inheritance with each role for the platform
  self.inheritance_column = :role

end

2)我得到了一个Rp子模型,它使用role =“Rp”对User进行子类化,并将其与Venue模型联系起来:

class Rp < User
  has_and_belongs_to_many :venues
end

3)我得到了Venue模型,并将其与Rp模型联系起来:

class Venue < ActiveRecord::Base
  has_and_belongs_to_many :rps
end

4)我按顺序获得了连接表并进行了迁移:

class CreateJoinTableVenueRp < ActiveRecord::Migration
  def change
    create_join_table :Venues, :Rps do |t|
      t.index [:venue_id, :rp_id]
      t.index [:rp_id, :venue_id]
    end
  end
end

这为我提供了数据库中的Rps_Venues连接表。一切都很好:

enter image description here

但是,我希望能够做到

Venue.first.rps

Rp.first.venues

但是当我这样做时,Active Records会以某种方式进入bananers并且没有抓住正确的连接表,而是试图将场地与Rp超类匹配,用户:

Loading development environment (Rails 4.2.5.1)
[1] pry(main)> Venue.first.rps
  Venue Load (0.9ms)  SELECT  "venues".* FROM "venues"  ORDER BY "venues"."id" ASC LIMIT 1
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR:  **relation "users_venues" does not exist**
LINE 5:                WHERE a.attrelid = '"users_venues"'::regclass
                                          ^
:               SELECT a.attname, format_type(a.atttypid, a.atttypmod),
                     pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod
                FROM pg_attribute a LEFT JOIN pg_attrdef d
                  ON a.attrelid = d.adrelid AND a.attnum = d.adnum
               WHERE a.attrelid = '"users_venues"'::regclass
                 AND a.attnum > 0 AND NOT a.attisdropped
               ORDER BY a.attnum
from /home/null/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/postgresql_adapter.rb:592:in `async_exec'
[2] pry(main)> Rp.first.venues
  Rp Load (1.4ms)  SELECT  "users".* FROM "users" WHERE "users"."role" IN ('Rp')  ORDER BY "users"."id" ASC LIMIT 1
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR:  **relation "users_venues" does not exist**
LINE 5:                WHERE a.attrelid = '"users_venues"'::regclass
                                          ^
:               SELECT a.attname, format_type(a.atttypid, a.atttypmod),
                     pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod
                FROM pg_attribute a LEFT JOIN pg_attrdef d
                  ON a.attrelid = d.adrelid AND a.attnum = d.adnum
               WHERE a.attrelid = '"users_venues"'::regclass
                 AND a.attnum > 0 AND NOT a.attisdropped
               ORDER BY a.attnum
from /home/null/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/postgresql_adapter.rb:592:in `async_exec'

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

Aaaaa和我们走了,我知道我很接近:

只需像这样指定:join_table

class Rp < User
  has_and_belongs_to_many :venues, join_table: "Rps_Venues"
end

class Venue < ActiveRecord::Base
  has_and_belongs_to_many :rps, join_table: "Rps_Venues"
end

现在一切正常。