Thursday, 5 September 2013

Rails model should have children of same type?

Rails model should have children of same type?

In my Rails app users can create submissions, and I'm creating a feature
so users can make highlights and have nested submissions. Submissions have
a parent_id attribute, which is null unless they do have a parent
submission, in which case it's the parent's id.
Here's submission.rb:
class Submission < ActiveRecord::Base
belongs_to :user
belongs_to :folder
belongs_to :parent, :class_name => 'Submission'
has_many :children, :class_name => 'Submission', :foreign_key => 'parent_id'
attr_accessible :content, :title, :user_id, :folder_id, :parent_id
def self.search(search)
if search
where('name LIKE ?', "%#{search}%")
else
scoped
end
end
end
I have the feature working properly to add the correct parent_id to child
submissions, but I'm not sure how to create a migration to get the
"children" attribute displaying with all children in it. How do I write
one to do this?

No comments:

Post a Comment