10

Tôi có thể làm một số thứ như thế này trong Rails không?Trộn trong mô-đun với attr_accessible, has_one, has_many

module Authored 
    belongs_to :user 
    attr_accessible creation_date 
end 

class Line < ActiveRecord::Base 
    include Authored 
end 

class Document < ActiveRecord::Base 
    include Authored 
end 

class User < ActiveRecord::Base 
    has_many :creations, :class_name => 'Authored' 
end 

Hoặc tôi có cần sử dụng thừa kế đơn giản, ngay cả khi lớp được ủy quyền của tôi có phân cấp lớp khác nhau?

Trả lời

23
module Authored 
    extend ActiveSupport::Concern 

    included do 
    belongs_to :user 
    attr_accessible :creation_date 
    end 
end 

class Line < ActiveRecord::Base 
    include Authored 
end 

class Document < ActiveRecord::Base 
    include Authored 
end 

Để biết thêm về ActiveSupport::Concern, http://api.rubyonrails.org/classes/ActiveSupport/Concern.html

+1

Chỉ cần những gì tôi cần, nhờ :) Có vẻ như nó sẽ không làm việc ra cho has_many trong tài khoản của tôi. Tôi có thể cần phải đối phó với nó bằng cách sử dụng một hiệp hội đa hình. –

Các vấn đề liên quan