Ruby on Rails 3 (3.1) ActiveModel Associations (tableless nested models)

前端 未结 3 1557
不知归路
不知归路 2021-01-12 07:01

How to impliment ActiveModel associations (tableless nested models)?

For example:

book has many chapters

With ActiveRecord I would

3条回答
  •  春和景丽
    2021-01-12 07:54

    You can check out this answer for another way to do it.

    class Tableless < ActiveRecord::Base
        def self.columns() @columns ||= []; end
    
        def self.column(name, sql_type = nil, default = nil, null = true)
            columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type.to_s, null)
        end 
    
        attr_accessor :id, :name, :value
    
        has_many :stuff_things
        has_many :things, :through => :stuff_things
    
    end
    

提交回复
热议问题