通过Rails中的同一个表进行多个has_many关联

时间:2010-08-10 11:32:47

标签: ruby-on-rails activerecord associations has-many-through

我有以下数据库架构:

alt text http://img843.imageshack.us/img843/3410/databasediagram.png

我希望能够做到这样的事情:

dog.head << Feature.new(...)
dog.tail << Feature.new(...)

我是Rails的新手,所以我并不总是100%确定我写的是什么,但是我尝试遵循Dog类的声明,并且失败了:):

class Dog < ActiveRecord::Base
  has_many :features, :through=>:dog_features
  has_many :head_features, :through=>:dog_features, :class_name=>'Feature', :conditions=>{:group=>1}
  has_many :tail_features, :through=>:dog_features, :class_name=>'Feature', :conditions=>{:group=>2}
end

0 个答案:

没有答案