ruby on rails - ActiveRecord Polymorphic association -


i have project allows users have conversations other users. conversations can contain multiple users via userconversations modal. userconversations need polymorphic can belong chatrooms. however, when add polymorphic association relationships break.

class user < applicationrecord   has_many :user_conversations, dependent: :destroy   has_many :conversations, through: :user_conversations end  class userconversation < applicationrecord   belongs_to :user   belongs_to :parent, polymorphic: true   has_many :conversations, through: :user_conversations end  class conversation < applicationrecord   has_many :messages, as: :context, dependent: :destroy   has_many :user_conversations, as: :parent, dependent: :destroy   has_many :users, through: :user_conversations end  class chatroom < applicationrecord   belongs_to :venue   has_many :messages, as: :context, dependent: :destroy   has_many :user_conversations, as: :parent, dependent: :destroy   has_many :users, through: :user_conversations end   2.3.1 :009 > user = user.first   user load (1.8ms)  select  "users".* "users" order "users"."id" asc  limit $1  [["limit", 1]]  => #<user id: 1, name: "loi tran", email: "loi@coderschool.vn",  password_digest: "$2a$10$edz6iytazvk4qnvjfihpp.3fheihdv0bwuvqrtjjk86...",          image_url: "https://scontent.fsgn5-2.fna.fbcdn.net/v/t1.0-1/p3...",          created_at: "2017-08-21 07:20:11", updated_at: "2017-08-29 18:47:04", city:  "tallahassee", state: "florida", position: "getting yelled at", school:  "florida state university", quote: "if easy, it.",  avatar: nil, last_name: "tran", first_name: "loi">  2.3.1 :010 > user.user_conversations   userconversation load (0.8ms)  select  "user_conversations".*  "user_conversations" "user_conversations"."user_id" = $1 limit $2   [["user_id", 1], ["limit", 11]]  => #<activerecord::associations::collectionproxy [#<userconversation id: 453, user_id: 1, created_at: "2017-09-10 06:01:27", updated_at: "2017-09-10 06:01:27", parent_type: "chatroom", parent_id: 8>, #<userconversation id: 454, user_id: 1, created_at: "2017-09-10 06:02:22", updated_at: "2017-09-10 06:02:22", parent_type: "conversation", parent_id: 318>]>  2.3.1 :011 > user.conversations nomethoderror: undefined method `klass' nil:nilclass did mean?  class     (irb):11 

i'm using rails 5.1.3 & ruby 2.3.1

i need user.conversations work. please help!

use 2 join models instead, both simpler , avoid major cons of polymorphism:

  • no foreign key support db not know table association points to.
  • joins tricky since have query table know table join. bad join table.

since join tables consist of 2 columns , model has little logic using polymorphism not give headaches.

class user < applicationrecord   has_many :user_conversations, dependent: :destroy   has_many :conversations, through: :user_conversations   has_many :chatroom_users, dependent: :destroy   has_many :chatrooms, through: :chatroom_users end  class userconversation < applicationrecord   belongs_to :user   belongs_to :conversation end  class conversation < applicationrecord   has_many :messages, as: :context, dependent: :destroy   has_many :user_conversations, dependent: :destroy   has_many :users, through: :user_conversations end  class chatroomuser < applicationrecord   belongs_to :user   belongs_to :chatroom end  class chatroom < applicationrecord   belongs_to :venue   has_many :messages, as: :context, dependent: :destroy   has_many :chatroom_users, dependent: :destroy   has_many :users, through: :user_chatrooms end 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -