Ruby on rails 管理rails中两个类的多个关联
我想管理一对多类型的关联,用于用户和答案,以及多对多类型的关联,用于相同两个模型(即用户和答案)的投票管理 那么,如何同时维护这两个关联呢 这段代码是我想要实现的Ruby on rails 管理rails中两个类的多个关联,ruby-on-rails,associations,Ruby On Rails,Associations,我想管理一对多类型的关联,用于用户和答案,以及多对多类型的关联,用于相同两个模型(即用户和答案)的投票管理 那么,如何同时维护这两个关联呢 这段代码是我想要实现的 class User < ActiveRecord::Base has_many :answers #For the answers of particular user has_and_belongs_to_many :answers #For the answers upvoted by a particul
class User < ActiveRecord::Base
has_many :answers #For the answers of particular user
has_and_belongs_to_many :answers #For the answers upvoted by a particular user
end
class Answer < ActiveRecord::Base
belongs_to :user #Author of the answer
has_and_belongs_to_many :users #For those who upvoted the answer
end
class用户
您应该为另一个关联指定不同的名称,但是还需要指定类名
has_many :answers
has_and_belongs_to_many :voted_answers, class_name: "Answer"
不清楚你在问什么。@DaveNewton实际上,当用户“u”将投票时,我想将该答案添加到u.answers中(他没有投票支持他写的答案),但用户u.answers不能用于两个关联:(那么你需要两个关联,而不仅仅是一个。查看AR指南中的选项。