Ok so I built this application a while which was a rails 2.3.5 application and i used this syntax
@user = User.new
@user.build_company
and I totally forgot what this meant and how to convert to rails 3 syntax
class User < ActiveRecord::Base
has_one :company
class Company < ActiveRecord::Base
belongs_to :user
I vaguely remember that rails will use build_ and do something but i cant remember and I cant seem to find the documentation anywhere
.build_
will make a new object of the associated type (a Company) that is linked to the object it's called on (a User), but which is not yet saved. AFAIK this hasn't changed in Rails 3.
See this question: Ruby on Rails. How do I use the Active Record .build method in a :belongs to relationship?
And the docs it points to: http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html
That is correct syntax for Rails 3.
has_one relationship:
@parent.build_child
has_many relationship:
@parent.children.build
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.