cgriego / active_attr

What ActiveModel left out
MIT License
1.19k stars 86 forks source link

Associations #7

Open cgriego opened 12 years ago

cgriego commented 12 years ago

Models have associations with other models, and attributes don't cover this case very well (especially when initializing a model with a deeply nested hash).

jmacdonald commented 10 years ago

This would be great, especially when paired with typecasting checks to ensure that associated objects are of the correct class.

Gaelan commented 10 years ago

Something else that would be extremely awesome (but maybe a bit hard to implement) would be to automatically serialize when an ActiveRecord model declares an association to it.

cgriego commented 10 years ago

@Gaelan You should be able to use ActiveRecord's serialized columns/attributes to do this today. I'd be interested in hearing either way if it works or doesn't for you. ActiveAttr may need to become more explicit about its YAML serialization for best results.

class Person
  include ActiveAttr::Model
  attribute :first_name, :type => String
end

class Post < ActiveRecord::Base
  serialize :author
end

Post.new(:author => Person.new(:first_name => "Chris")).save!
Gaelan commented 10 years ago

@cgriego OK