Skip to content

Instantly share code, notes, and snippets.

@pjb3
Created October 17, 2012 15:08
Show Gist options
  • Save pjb3/3906056 to your computer and use it in GitHub Desktop.
Save pjb3/3906056 to your computer and use it in GitHub Desktop.
By using actual class constants for classes, instead of strings, doesn't the create a mutual dependency problem?
require 'virtus'
class Post
include Virtus
attribute :title, String
attribute :comments, Array[Comment]
end
class Comment
include Virtus
attribute :post, Post
attribute :body, String
end
# I might want to be able to do this
puts Comment.new(:body => "First Comment", :post => {:title => "First Post"})
# or this
puts Post.new(:title => "First Post", :comments => [{:body => "First Comment"}, {:body => "Second Comment"}]).inspect
# but you can't because depending on which model you define first, Post or Comment, the other one isn't defined yet
# doesn't it need to work like this?
class Post
include Virtus
attribute :title, String
attribute :comments, Array['Comment']
end
class Comment
include Virtus
attribute :post, 'Post'
attribute :body, String
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment