Has_many:通过反向访问?

发布于 2024-12-17 07:34:12 字数 2482 浏览 2 评论 0原文

不完全是一个错误,但我认为我在这里遗漏了一些重要的东西..

 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
 => []   

Not exactly an error, but I think I am missing something important here..

 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

So I can do something like:

 user_test.teams << team_test

and I expect that after that I should be able to do something like:

team_test.users 

it will list user_test among all others.. But it does not..

What am I missing?

Thanks!

EDIT::

 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
 => []   

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

却一份温柔 2024-12-24 07:34:12

这很奇怪...... 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/ 没有已保存

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

您可能处于情况 2.b 或情况 3。

This is strange... Are user_test and team_test saved or only initialized?

1/ the both are saved:

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/ only one is saved:

a) the saved model is the one which 'received' the other:

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) the saved model is the one which 'is received' by the other:

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

3/ none is saved

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

You might be in case 2.b or in case 3.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文