2016-08-05 13 views
0

次の関連付けを設定しようとしています。has_many関係は機能しません。

task.rb

class Task < ApplicationRecord 
    belongs_to :user 
    has_one :organziation, through: :user 
end 

user.rb

class User < ApplicationRecord 
    belongs_to :organization 
    has_many :tasks 
    accepts_nested_attributes_for :organization 
end 

organization.rbここ
class Organization < ApplicationRecord 
    has_many :users 
end 

である私のコンソール出力:

2.3.0 :001 > t = Task.last 
    Task Load (0.2ms) SELECT "tasks".* FROM "tasks" ORDER BY "tasks"."id" DESC LIMIT ? [["LIMIT", 1]] 
=> #<Task id: 3, name: "Register Students", created_at: "2016-08-05 20:00:34", updated_at: "2016-08-05 20:00:34", user_id: 5> 
2.3.0 :002 > t.user 
    User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 5], ["LIMIT", 1]] 
=> #<User id: 5, email: "[email protected]", created_at: "2016-08-05 19:59:56", updated_at: "2016-08-05 20:00:07", organization_id: 1, admin: false> 
2.3.0 :003 > t.organization 
NoMethodError: undefined method `organization' for #<Task:0x007fbf8bc6d1 

タスクからタスク組織にディレクトリを取得できます。

答えて

2

アソシエーション名が入力されています。それが好きな修正:

has_one :organization, through: :user 

あなたはorganziationを持っていた、私は組織にそれを変更しました。

私はあなたが見ているので、あなたがすべてのタスクを取得するためにも、組織が破壊された場合は、ここで dependent: :destroyを使用した場合、それに関連するすべてのタスクがあまりにも破壊されなければならない、あまりにも組織モデルに

has_many :tasksを追加すべきだと思う

+0

私はそれが起こるときにそれが嫌い! :/ – Lumbee

+0

何度も私に起こった:( –

+0

余分な目をつけてくれてありがとう – Lumbee

1

この組織に関連して、あなたはそれを必要とするかもしれません

+0

良い点ドルイド! – Lumbee

関連する問題