English 中文(简体)
如果X有1个Y,你是否还必须说Y属于X?
原标题:If X has_one Y, do you have to also say Y belong_to X?
最佳回答

具有: 添加相关物体检索和收集方法。

属于: 添加该物体所持有的单一相关物体的检索和查询方法

so, if you dont require to use methods on both the associated objects you may choose not the set the association accordingly.But its mandatory to put the foreign key in the model where the one instance is associated to one and only one instance of the other object that is: belongs_to model.

解释:

让我们考虑像样:

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:	emp>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:	emp>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>
问题回答

你们不要再说什么?

所有方法都是在它们重新使用的类别中增加额外的方法/元数据:如果你不想要/利用这些方法,就不需要添加这些方法。

添加的方法见:http://guides.rubyonrails.org/association_basics.html#detailed-association-vis”rel=“nofollow” 。





相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签