我有以下内容:
经销商表
admin_id = 7 (correct)
type = Reseller (also correct)
用户表
owner_id = 6 (correct)
owner_type = BaseReseller (is this OK?)
type = ResellerUser (correct)
问题似乎在于多态关联,即使在数据库中一切正常,如果我调用@reseller.admin我也没有。这是因为我们有两个STI层次结构,它们之间存在多态关联吗?这是否支持Rails,如果没有,有没有人知道如何解决/返工?提前致谢。
代码在这里:
class BaseReseller < ActiveRecord::Base
set_table_name "resellers"
acts_as_nested_set
belongs_to :admin, :polymorphic => true
has_many :users, :as => :owner
end
class IPSP < BaseReseller
end
class Agent < BaseReseller
end
class Reseller < BaseReseller
end
class User < ActiveRecord::Base
belongs_to :owner, :polymorphic => true
has_one :administrable_owner, :as => :admin
end
class ResellerUser < User
end
class ResellersController < ApplicationController
...
def create
@reseller = Reseller.new(params[:reseller])
admin_user = ResellerUser.find(params[:reseller][:admin_id])
@reseller.admin = admin_user
@reseller.users << admin_user unless @reseller.users.include?(admin_user)
if @reseller.save
redirect_to @reseller
else
render :action => :new
end
end
end
DB Schema:
CREATE TABLE users (
id int(11) NOT NULL AUTO_INCREMENT,
owner_id int(11) DEFAULT NULL,
owner_type varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
type varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
PRIMARY KEY (id),
KEY index_users_on_owner_id (owner_id),
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE resellers (
id int(11) NOT NULL AUTO_INCREMENT,
admin_id int(11) DEFAULT NULL,
type varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
parent_id int(11) DEFAULT NULL,
rgt int(11) DEFAULT NULL,
lft int(11) DEFAULT NULL,
depth int(11) DEFAULT NULL,
PRIMARY KEY (id),
KEY index_resellers_on_admin_id (admin_id),
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
答案 0 :(得分:1)
如果您认为双重多态性导致问题,我建议暂时从其中一个层次结构(然后是另一个)中删除多态性,看看是否仍有问题。这将缩小问题的范围。
此外,您的架构defs是什么样的?您可能缺少一个外键列。