Has_and_belongs_to_many 和单一 Table 继承
Has_and_belongs_to_many and Single Table Inheritance
我需要一些帮助。我觉得让这个工作起来很傻,但我真的想不通。
我需要在子模型和另一个模型之间进行连接 table。
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 子模型,它用角色 = "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) 我按顺序获得了连接 table 并迁移了:
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 连接 table。一切都很好:
不过,我希望能够做到
Venue.first.rps
和
Rp.first.venues
但是当我这样做时,不知何故 Active Records 变得糟糕并且没有抓住正确的连接 table,而是试图将场所与 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'
有人可以帮忙吗?
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
现在一切正常。
我需要一些帮助。我觉得让这个工作起来很傻,但我真的想不通。
我需要在子模型和另一个模型之间进行连接 table。
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 子模型,它用角色 = "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) 我按顺序获得了连接 table 并迁移了:
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 连接 table。一切都很好:
不过,我希望能够做到
Venue.first.rps
和
Rp.first.venues
但是当我这样做时,不知何故 Active Records 变得糟糕并且没有抓住正确的连接 table,而是试图将场所与 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'
有人可以帮忙吗?
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
现在一切正常。