I am in rails 3.1. I have the following models
class Tool < ActiveRecord::Base
has_many :comments
end
class Comment < ActiveRecord::Base
belongs_to :tool
has_many :relationships
has_many :advantages, :through => :relationships, :source => :resource, :source_type => 'Advantage'
has_many :disadvantages, :through => :relationships, :source => :resource, :source_type => 'Disadvantage'
end
class Relationship < ActiveRecord::Base
belongs_to :comment
belongs_to :resource, :polymorphic => true
end
class Disadvantage < ActiveRecord::Base
has_many :relationships, :as => :resource
has_many :comments, :through => :relationships
end
class Advantage < ActiveRecord::Base
has_many :relationships, :as => :resource
has_many :comments, :through => :relationships
end
In short, A Tool
has many comments
. A Comment
inturn is associated with Advantages
and Disadvantages
. So in my tool/show
page, I would list out all the comments.
But if I have to add comment to the tool page, there would be a form which has a textarea for comment and two multi select list boxes
for advantages and disadvantages.
And here is the catch, if the user wanna select from existing adv/disadv, the user can select from the list box or if the user wants to a add a new adv/disadv he can type it and add it, so that it is saved thru an ajax call and the new adv/disadv is added to the list box. How am I supposed to do this?