可以belongs_to的工作,而的has_many或HAS_ONE [英] can Belongs_to work without has_many or has_one
问题描述
我学习belongs_to的关联,我已经使用以下机型,在每一个订单是属于客户的,所以我用belongs_to的以模型给它的错误,同时创造秩序
未定义的方法'订单'为#
块引用>
在我使用的has_many:订单的客户模型它工作得很好,为什么它
不只有belongs_to的工作及其与工作的has_many:客户模式,但不与订单
HAS_ONE:为了客户控制它赋予相同的上述错误先谢谢了。
型号: - order.rb
类订单< ActiveRecord的::基地
belongs_to的:客户
attr_accessible:order_date的,:CUSTOMER_ID
结束型号: - customer.rb
类客户和LT; ActiveRecord的::基地
attr_accessible:名称
结束控制器: - orders.rb
DEF创建
@customer = Customer.find_by_name(PARAMS [:名字])
@order = @ customer.orders.new(:order_date的= GT; PARAMS [:订购日期]) 做的respond_to |格式|
如果@ order.save
format.html {redirect_to的@order,通知:订单已成功创建。 }
format.json {渲染JSON:@order,状态:创建,地点:@order}
其他
format.html {渲染动作:新}
format.json {渲染JSON:@ order.errors,状态:unprocessable_entity}
结束
结束
结束
解决方案从技术上说,
belongs_to的
的将会的不匹配的工作的has_many
或HAS_ONE
。如果,例如,你说订单belongs_to的:客户
,你可以叫。客户
的Order对象,并得到一个Customer对象。
什么你不能做的就是调用.orders
在客户没有告诉它,它的has_many:订单
(或.order
,在HAS_ONE
),因为是由的has_many <的情况下/ code>声明。
这是说,我想不出你会永远只想指定半个关系的任何原因。这是一个可怕的设计选择,你不应该这样做。
编辑:
HAS_ONE
不会创建的has_many 的
.collection
方法code>一样。每的指南:
4.2.1方法采用HAS_ONE添加
在声明HAS_ONE协会,声明类
自动获得相关协会四种方法:协会(force_reload = FALSE)
协会=(副)
build_association(属性= {})
create_association(属性= {})
块引用>您会注意到,有没有
。新
在此列。如果你想添加一个关联的对象,你可以使用customer.build_order()
或customer.order = Order.new()
。I am studying Belongs_to association, I have used following models, in that every order belongs to the customer, so I have used belongs_to in order model it giving error while creating order
undefined method `orders' for #
when I use has_many :orders in customer model it works fine, why it does not work with only belongs_to
Its work with has_many :orders in customer model but not with has_one : order in customer controller it giving same above error.
thanks in advance.
Model :- order.rb
class Order < ActiveRecord::Base belongs_to :customer attr_accessible :order_date, :customer_id end
Model :- customer.rb
class Customer < ActiveRecord::Base attr_accessible :name end
controller :- orders.rb
def create @customer = Customer.find_by_name(params[:name]) @order = @customer.orders.new(:order_date => params[:orderdate] ) respond_to do |format| if @order.save format.html { redirect_to @order, notice: 'Order was successfully created.' } format.json { render json: @order, status: :created, location: @order } else format.html { render action: "new" } format.json { render json: @order.errors, status: :unprocessable_entity } end end end
解决方案Technically,
belongs_to
will work without a matchinghas_many
orhas_one
. If, for instance, you say that Orderbelongs_to :customer
, you can call.customer
on an Order object, and get a Customer object. What you can't do is call.orders
on a Customer without telling it that ithas_many :orders
(or.order
, in the case ofhas_one
), because that method is created by thehas_many
declaration.That said, I can't think of any reason you would ever want to only specify half of a relation. It's a terrible design choice, and you should not do it.
Edit:
has_one
doesn't create the.collection
methods thathas_many
does. Per the guide:4.2.1 Methods Added by has_one
When you declare a has_one association, the declaring class automatically gains four methods related to the association:
association(force_reload = false) association=(associate) build_association(attributes = {}) create_association(attributes = {})
You'll note that there's no
.new
on that list. If you want to add an associated object, you can usecustomer.build_order()
, orcustomer.order = Order.new()
.这篇关于可以belongs_to的工作,而的has_many或HAS_ONE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!