0

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?

Community
  • 1
  • 1
Andrew K
  • 1,571
  • 1
  • 17
  • 25
  • 1
    why `:Article`, it's either `Article`, `'Article'`, or `:article` – Mohammad AbuShady Sep 08 '14 at 22:50
  • What's the relationship between `articles` and `article_relations` tables? For `has_many :effects, :through => :article_relations...` to be meaningful for `Article`, there needs to be a relationship between `Article` and `ArticleRelation`. – lurker Sep 09 '14 at 00:39
  • Articles are related to themselves through the article_relations table. For example: Article A can be the "cause" of article E. Thus A.effects should include article E and E.causes should include article A – Andrew K Sep 09 '14 at 00:49

1 Answers1

0

Indeed, you need the capital :Article or 'Article' in ArticleRelation

Further, I was able to successfully create the relation with:

class ArticleRelation < ActiveRecord::Base
  belongs_to :user
  belongs_to :cause, :class_name => :Article
  belongs_to :effect, :class_name => :Article
end

and in the Article model:

class Article < ActiveRecord::Base
  belongs_to :user
  has_many :effects, :through => :effect_relations, :foreign_key => :cause_id
  has_many :causes, :through => :cause_relations, :foreign_key => :effect_id
  has_many :cause_relations, :class_name => :ArticleRelation, :foreign_key => :effect_id
  has_many :effect_relations, :class_name => :ArticleRelation, :foreign_key => :cause_id
end

Hope this helps anybody else running into this problem. Now I can do a.causes and it returns an array populated with Article types!

Andrew K
  • 1,571
  • 1
  • 17
  • 25