2013-05-22 29 views
0

我想在Rails中进行STI。示波器不支持STI

class AbstractUser < ActiveRecord::Base 
    self.table_name = 'users' 

    belongs_to :organization, :inverse_of => :users 

    # reporter user 
    has_many :requests, :dependent => :destroy 

    # startup user 
    has_many :responses, :dependent => :destroy 
    has_many :startup_requests, :through => :responses, :source => :request 

    scope :reporters, where(:type => 'Reporter') 
    scope :startup_employees, where(:type => 'Startup') 
    scope :on_waitlist, where(:waitlist => true) 
    scope :not_on_waitlist, where(:waitlist => false) 

end 

require 'rfc822' 

class User < AbstractUser 
    devise :database_authenticatable, :registerable, 
     :recoverable, :rememberable, :trackable, :confirmable 

    validates :name, :presence => true 
    validates :surname, :presence => true 
    validates :title, :presence => true 
    validates :password, :presence => true, :length => { :minimum => 8 } 
    validates :email, :presence => true, :format => { :with => RFC822::EMAIL_REGEXP_WHOLE } 

    attr_accessible :name, :surname, :title, :organization, 
        :email, :password, :fullname 
end 

require 'rfc822' 

class UserForAdmin < AbstractUser 
    validates :email, :presence => true, :format => { :with => RFC822::EMAIL_REGEXP_WHOLE } 
    validates :organization_id, :presence => true 

    attr_accessible :name, :surname, :title, :organization, :email, 
        :password, :fullname, :password_confirmation, :type, 
        :organization_id, :waitlist, :invitation_token 
end 

而且还有一些问题,这些范围。

Couldn't find UserForAdmin with id=7 [WHERE "users"."type" IN ('UserForAdmin') AND "users"."waitlist" = 'f'] 

我也试图把这些示波器在UserForAdmin,而不是AbstractUser具有相同的结果。我(可能)需要范围而不是自定义方法,因为我在ActiveAdmin中使用它们。我该如何解决这个问题?

+0

发生错误的条件是什么?你在打哪个范围?你期望看到什么? –

+0

当我尝试在ActiveAdmin控制器中如果UserForAdmin.not_on_waitlist.find(params [:id])''时发生。 – ciembor

+0

它试图做错的查询?看起来它正在做你要求它做的事情,所以你需要说出你期望它做什么。 –

回答

1

如果您不想接收所有用户,则需要使用基类进行查询。在一个简单的例子:

class Animal < ActiveRecord::Base 
end 

class Dog < Animal 
end 

class Cat < Animal 
end 

Dog.create 
Cat.create 

Animal.all 
=> [dog, cat] 

Dog.all 
=> [dog] 

Cat.all 
=> [cat] 

所以,你的情况,你会想:

AbstractUser.not_on_waitlist.find(params[:id]) 

如果该用户是UserForAdmin您会收到UserForAdmin类的对象。如果它只是一个用户,您将收到类User的一个对象

+0

@ciembor是否解决了您的问题? –