开发者

Problem with relations

开发者 https://www.devze.com 2023-03-13 22:31 出处:网络
Hey I have a problem with the extension of the existing dependency models. Well, according to between the models are as follows:

Hey I have a problem with the extension of the existing dependency models. Well, according to between the models are as follows: I have a User model:

class User <ActiveRecord::Base 
 has_many :words, :through => :memo_words 
 has_many :memo_words, :dependent => :destroy 
end 

class MemoWord 
  belongs_to :user 
  belongs_to :word 
end 

class Word 
  has_many :translations, :dependent => :destroy 
  has_many :memo_words, :dependent => :destroy 
end 

class Translation 
  belongs_to :word 
end 

This is a diagram now: http://img221.imageshack.us/img221/4232/przedik.png

Word model represents a word in one language and the model represents the Translation translation of individual words. I want to resolve the situation when a r开发者_运维问答ecord in the table and the Word, there is no record Translation (word has no translation). I want to allow for user to add their own translations, but translations done by adding a local (per user). Due to the lack of relationship between Translation and User, the User is not possible to add words. And I question whether a good solution is to add the model UserTranslation:

UserTranslation class 
  belongs_to :word 
  belongs_to :user 
end 

And diagram with situation after change. http://img851.imageshack.us/img851/7269/75031527.png

Which would have the same functionality as the model of Translation. In practice, I would have to copy the model to UserTranslation Translation by adding only 'belongs_to :user'. Is there a better approach to the problem


I would suggest that in your current scheme consider UserTranslations to be STI for Translations so -

class UserTranslation < Translation
  belongs_to :user
end

This way all user translated words will be saved inside "translations" table but with type "user_translations". Then you may make id unapproved by default and build admin side approval functions.

This way, @word.translation would yield either translation or user_translation object.

0

精彩评论

暂无评论...
验证码 换一张
取 消