我目前正在阅读A Guide to Active Record Associations。
我知道您在has_one或belongs_to之间做出选择,具体取决于您希望如何布置关联。
但我不完全清楚以下是否存在差异。
那么有没有必要明确说出X has_one Y和Y belongs_to X? 或者说X has_one Y或Y belongs_to X总是足够的。
答案 0 :(得分:3)
答案 1 :(得分:2)
has_many:添加检索和查询关联对象集合的方法。
belongs_to:添加用于检索和查询此对象拥有id的单个关联对象的方法
所以,如果您不需要在关联对象上使用方法,您可以选择不相应地设置关联。但是必须将外键放在模型中,其中一个实例与一个实例关联且只有一个实例另一个对象是:'belongs_to'模型。
阐释:
让我们考虑以下架构:
create_table "cars", :force => true do |t|
t.integer "user_id"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "users", :force => true do |t|
t.datetime "created_at"
t.datetime "updated_at"
end
和模型为:
class User < ActiveRecord::Base
end
class Car < ActiveRecord::Base
belongs_to :user
end
结果是:
D:\temp>rails c
Loading development environment (Rails 3.0.10)
irb(main):001:0> U = User.first
=> #<User id: 1, created_at: "2011-11-22 06:43:04", updated_at: "2011-11-22 06:43:04">
irb(main):002:0> C = Car.first
=> #<Car id: 1, user_id: 1, created_at: "2011-11-22 06:43:34", updated_at: "2011-11-22 06:43:34">
irb(main):003:0> C.user
=> #<User id: 1, created_at: "2011-11-22 06:43:04", updated_at: "2011-11-22 06:43:04">
irb(main):004:0> U.cars
NoMethodError: undefined method `cars' for #<User:0x3d19970>
from C:/Ruby/lib/ruby/gems/1.9.1/gems/activemodel-3.0.10/lib/active_model/attribute_methods.rb:392:in `method_missing'
from C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.10/lib/active_record/attribute_methods.rb:46:in `method_missing'
from (irb):4
from C:/Ruby/lib/ruby/gems/1.9.1/gems/railties-3.0.10/lib/rails/commands/console.rb:44:in `start'
from C:/Ruby/lib/ruby/gems/1.9.1/gems/railties-3.0.10/lib/rails/commands/console.rb:8:in `start'
from C:/Ruby/lib/ruby/gems/1.9.1/gems/railties-3.0.10/lib/rails/commands.rb:23:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
irb(main):005:0> exit
如果模型如下:
和模型为:
class User < ActiveRecord::Base
has_many :cars
end
class Car < ActiveRecord::Base
end
结果是:
D:\temp>rails c
Loading development environment (Rails 3.0.10)
irb(main):001:0> U = User.first
=> #<User id: 1, created_at: "2011-11-22 06:43:04", updated_at: "2011-11-22 06:43:04">
irb(main):002:0> C = Car.first
=> #<Car id: 1, user_id: 1, created_at: "2011-11-22 06:43:34", updated_at: "2011-11-22 06:43:34">
irb(main):003:0> U.cars
=> [#<Car id: 1, user_id: 1, created_at: "2011-11-22 06:43:34", updated_at: "2011-11-22 06:43:34">]
irb(main):004:0> C.user
NoMethodError: undefined method `user' for #<Car:0x41524c8>
from C:/Ruby/lib/ruby/gems/1.9.1/gems/activemodel-3.0.10/lib/active_model/attribute_methods.rb:392:in `method_missing'
from C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.10/lib/active_record/attribute_methods.rb:46:in `method_missing'
from (irb):4
from C:/Ruby/lib/ruby/gems/1.9.1/gems/railties-3.0.10/lib/rails/commands/console.rb:44:in `start'
from C:/Ruby/lib/ruby/gems/1.9.1/gems/railties-3.0.10/lib/rails/commands/console.rb:8:in `start'
from C:/Ruby/lib/ruby/gems/1.9.1/gems/railties-3.0.10/lib/rails/commands.rb:23:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
irb(main):005:0>