Re: [Rails-core] Could Ruby Structs be useful to ActiveRecord?

100 views
Skip to first unread message

Steve Klabnik

unread,
Jul 21, 2012, 4:03:06 PM7/21/12
to rubyonra...@googlegroups.com
The DAO pattern is totally different than the ActiveRecord pattern, so
you probably won't see anything like this happen inside of AR itself.
Other ORMs may do something like this, though.

If your model is just a struct, then it has no 'domain' methods...

Maurizio De Santis

unread,
Jul 22, 2012, 12:20:43 PM7/22/12
to rubyonra...@googlegroups.com
What do you mean for 'domain' methods? Do you mean validations, etc? They can be implemented; the model could be a sort of "struct on steroids"... something like this:

module ActiveRecord
  class Struct < ::Struct
    def self.find
    end
    def self.all
    end
    # other various implementations
  end
  class StructFor
    def [](table_name)
       # retrieve table column names
       ActiveRecord:::Struct.new(*table_column_names)
    end
  end
end


model:
class User < ActiveRecord::StructFor[:users] # this returns ActiveRStruct.new(:firstname, :lastname, :active)

  def before_save
     # ...
  end

end

Steve Klabnik

unread,
Jul 22, 2012, 12:32:30 PM7/22/12
to rubyonra...@googlegroups.com
Yeah, you'd _have_ to do something like that. Then you're not
implementing the ActiveRecord pattern anymore, so I don't think it'd
really be appropriate.

> Active Record uses the most obvious approach, putting data access logic in the domain object.
>
> - http://martinfowler.com/eaaCatalog/activeRecord.html
Reply all
Reply to author
Forward
0 new messages