无法为has_many:through创建别名

时间:2013-11-17 08:05:56

标签: ruby-on-rails

这是我的迁移代码:

1.用户

    create_table :users do |t|
      t.string :name
      t.string :password
      t.string  :department

      t.timestamps
    end

2.Ploy

    create_table :ploys do |t|
      t.string :name
      t.string :state
      t.string :description
      t.string :location
      t.float  :spend

      t.references :creator, index: true, class_name: "User"

      t.timestamps
    end

3.participants

   def change
    create_table :participants do |t|
      t.belongs_to :ploy
      t.belongs_to :user

      t.timestamps
    end

这是我的型号代码:

class User < ActiveRecord::Base
  has_one :create_ploys, class_name: "Ploy"

  has_many :participants
  has_many :ploys, through: :participants, source: :join_ploys
end

class Ploy < ActiveRecord::Base
  belongs_to :creator, class_name: "User"

  has_many :participants
  has_many :users, through: :participants, source: "joiners"
end

class Participant < ActiveRecord::Base
  belongs_to :user
  belongs_to :ploy
end

当我调用user.join_ploys时,但是给我一个错误: NoMethodError:未定义的方法`join_ploys'

所以我想也许有些事情是错的:来源,但我不知道该怎么做。

1 个答案:

答案 0 :(得分:0)

那么您在哪里为joiner和join_ploys定义模型或关联?你不能只创建一个:source而不定义它是什么。看看这个SO:Need help to understand :source option of has_one/has_many through of Rails