I have thoroughly read this stack overflow question.
I am still stuck trying to apply it to my own situation. I have an Article model and an Article Relation model for tying Articles to themselves via cause-effect relationship
create_table "article_relations", force: true do |t|
t.integer "cause_id"
t.integer "effect_id"
t.integer "user_id"
t.datetime "created_at"
t.datetime "updated_at" end
add_index "article_relations", ["user_id"], name:
"index_article_relations_on_user_id", using: :btree
create_table "articles", force: true do |t|
t.string "code"
t.text "description"
t.integer "user_id"
t.datetime "created_at"
t.datetime "updated_at"
end
Ideally I will want to be able to do:
a = Article.find(...)
a.causes
# [b,c,d]
a.effects
# [e,f,g]
e = Article.find(...)
e.causes
# [a,c,d]
e.effects
# [x,y,z]
In my models I've added:
class Article < ActiveRecord::Base
belongs_to :user
has_many :effects, :through => :article_relations, :source => :effect
has_many :causes, :through => :article_relations, :source => :cause
end
and for the article_relation class:
class ArticleRelation < ActiveRecord::Base
belongs_to :user
belongs_to :cause, :class_name => :Article
belongs_to :effect, :class_name => :Article
end
I'm pretty sure my schema is set up correctly, but I am not sure that my models are. What can I add so that I can achieve the above desired behavior? Is it possible to cascade deletes through :dependent=> destroy?