0

不完全是一個錯誤,但我覺得我缺少一些重要的東西在這裏..Has_many:通過反向訪問?

class Team < ActiveRecord::Base 
    has_many :groups 
    has_many :users, :through => :groups 

class User < ActiveRecord::Base 
    acts_as_authentic 
    has_many :groups 
    has_many :teams, :through => :groups 


class Group < ActiveRecord::Base 
    belongs_to :user 
    belongs_to :team 

所以我可以這樣做:

user_test.teams << team_test 

而且我希望以後我應該能夠這樣做:

team_test.users 

它會列出user_test所有其他人之間。但它沒有..

我錯過了什麼?

謝謝!

編輯::

ruby-1.9.3-p0 :001 > user_test = User.create 
(0.0ms) SAVEPOINT active_record_1 
(0.1ms) SELECT 1 FROM "users" WHERE "users"."persistence_token" =  '6f2890df599776198476630fad3db57b62606339d7ec2c1e96cc4081919789fa0a7cac5ffaed6b8f61f28f3ff2abd6ca890eb623c1b2d6718328d10527fa1566' LIMIT 1 
(0.0ms) ROLLBACK TO SAVEPOINT active_record_1 
    => #<User id: nil, username: nil, email: nil, crypted_password: nil, password_salt: nil, persistence_token: "6f2890df599776198476630fad3db57b62606339d7ec2c1e96c...", created_at: nil, updated_at: nil> 

ruby-1.9.3-p0 :002 > team_test = Team.create 
(0.0ms) SAVEPOINT active_record_1 
SQL (1.9ms) INSERT INTO "teams" ("created_at", "name", "personal", "project_id", "updated_at", "visible") VALUES (?, ?, ?, ?, ?, ?) [["created_at", Tue, 22 Nov 2011 23:09:28 UTC +00:00], ["name", nil], ["personal", false], ["project_id", nil], ["updated_at", Tue, 22 Nov 2011 23:09:28 UTC +00:00], ["visible", nil]] 
(0.0ms) RELEASE SAVEPOINT active_record_1 
=> #<Team id: 8, name: nil, created_at: "2011-11-22 23:09:28", updated_at: "2011-11-22 23:09:28", visible: nil, personal: false, project_id: nil> 


ruby-1.9.3-p0 :003 > user_test.teams << team_test 
(0.1ms) SAVEPOINT active_record_1 
(0.0ms) RELEASE SAVEPOINT active_record_1 
=> [#<Team id: 8, name: nil, created_at: "2011-11-22 23:09:28", updated_at: "2011-11-22 23:09:28", visible: nil, personal: false, project_id: nil>] 

    ruby-1.9.3-p0 :004 > user_test.teams 
    => [#<Team id: 8, name: nil, created_at: "2011-11-22 23:09:28", updated_at: "2011-11-22 23:09:28", visible: nil, personal: false, project_id: nil>] 

    ruby-1.9.3-p0 :005 > team_test.users 
User Load (0.1ms) SELECT "users".* FROM "users" INNER JOIN "groups" ON "users"."id" = "groups"."user_id" WHERE "groups"."team_id" = 8 
=> [] 
+0

保存

user = User.new team = Team.new users.teams << team user.teams # => team but not saved (i.e. id is nil) team.users # => [] (empty array) 

你可能是什麼?我想你可能需要編寫team_test.groups.users或類似的東西,包括加入模型。雖然不是很確定。 – John

+0

是的,我使用的Rails 3.1我試圖通過組訪問,但沒有.. – Stpn

回答

1

這就奇怪了......難道user_test和team_test保存或只初始化?

1 /兩者都保存:

user_test = User.create 
team_test = Team.create 
user_test.teams << team_test 
user_test.teams # => team_test among others 
team_test.users # => user_test among others 

2 /只有一個被保存:

a)所述保存的模型是 '接收' 其他的一個:

user = User.create 
team = Team.new 
user.teams << team 
# team is saved automatically 
user.teams # => team among others 
team.users # => user among others (because team was saved automatically) 

b)保存的模型是另一個'接收'的模型:

user = User.new 
team = Team.create 
user.teams << team 
user.teams # => return team 
team.users # => [] (empty array; the 'receiver' is not saved automatically) 

3 /都不是你使用Rails 3.1的情況下2.B情況下或者在3

+0

感謝您的回答,我更新了原始問題 – Stpn

+0

看看你的日誌:user_test沒有保存(id仍然爲零)。你的情況是2/b /。由於acts_as_authentic,這可能是一個問題。請問我有獎勵積分請大聲笑嗎? – Damien

+0

非常感謝,哦,我.. – Stpn