2
我想在rails中構建像數據模型一樣的twitter。這是我想出的。在軌道中的twitter模型協會
class User < ActiveRecord::Base
has_many :microposts, :dependent => :destroy
end
class Micropost < ActiveRecord::Base
belongs_to :user
has_many :mentions
has_many :hashtags
end
class Mention< ActiveRecord::Base
belongs_to :micropost
end
class Hashtag < ActiveRecord::Base
belongs_to :micropost
end
我應該在某處使用has_many關聯還是這樣準確?
編輯:最終的twitter MVC模型。
class User < ActiveRecord::Base
has_many :microposts, :dependent => :destroy
userID
end
class Micropost < ActiveRecord::Base
belongs_to :user
has_many :link2mentions, :dependent => :destroy
has_many :mentions, through: :link2mentions
has_many :link2hashtags, :dependent => :destroy
has_many :hashtags, through: :link2hashtags
UserID
micropostID
content
end
class Link2mention < ActiveRecord::Base
belongs_to :micropost
belongs_to :mention
linkID
micropostID
mentionID
end
class Mention < ActiveRecord::Base
has_many :link2mentions, :dependent => :destroy
has_many :microposts, through: :link2mentions
mentionID
userID
end
編輯2:簡潔而準確的解釋
http://railscasts.com/episodes/382-tagging?view=asciicast
我不確定我關注。兩個微博也可以有同樣的提及。我需要在這裏使用has_many模型嗎? –
是的,對於提及 – mihai
也一樣。哈希標籤表的用途是什麼。如果要存儲鏈接到hashtags表,爲什麼每個micropost都有多個hashtaggings? –