简体   繁体   中英

Model without attributes for has_many :through association

Consider the following I have messages that should be coupled together by thread. I have ended up with an architecture like this: Message belongs_to Thread and Thread has_many Messages . The key point here is to have an ability to get all related messages to the one i am working with.

class Message < ActiveRecord::Base
  attr_accessible :title, :body      
  belongs_to :thread
  has_many :messages, through: :thread


end

class Thread < ActiveRecord::Base
  has_many :messages
end

However I am a bit concerned about having a blank model. Model named Thread does not have any meaningful attributes in it. It is just a table with primary key sequence.

I had and idea to turn it into a tree but it looks too heavy too.

What do you think about it? How do you organize several models together with one blank model?

Why do you need thread table? I recommend you should use single table inheritance. Add

parent_message_id

in your messages table and get all the messages of a same thread using this id

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM