How to customize Rails 3 STI column name

后端 未结 4 1922
旧巷少年郎
旧巷少年郎 2020-12-31 11:08

I have a class named Post:

class Post < ActiveRecord::Base
end

I have a class named Question that inheriting f

4条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-31 11:11

    You can change the name of the single table inheritance column like so:

    class Post < ActiveRecord::Base
      self.inheritance_column = 'type_column_name'
    
    end
    

    However, there is no way to cause Rails to use integers instead of storing the actual type as a string, which makes me think that this may not be a great use case for single target inheritance. Perhaps a scope would suit you better instead:

    class Post < ActiveRecord::Base
      scope :questions, where(:post_type_id => 0)
      scope :answers, where(:post_type_id => 1)
    
    end
    
    @questions = Post.questions.all
    @answers = Post.answers.all
    

提交回复
热议问题