12月12日 has_many through:的interference, option
has_many :products, through: :cart_items, source: :product
build定义:collection.build(attributes = {}, …) 本例子中collection换成cart_items.
说明:这个method返回一个或多个new objects of the associated type.但关联的对象尚未保存。需要save.
Create定义:collection.create(attributes = {})
说明: 只返回一个新建关联对象,并自动被save.
source定义: 指定has_many :through关联的源关联name.只有无法从关联名中解出源关联的名称时才 需要设置
这个选项。
说明:这是has_many中的option选项附加
参考:Active Record asscoiation reference 关联参考
http://guides.rubyonrails.org/v2.3.11/association_basics.html#has-one-association-reference
4.3 has_many Association Reference
我的理解:一旦两个model建立一对多的关联,这个1的model自动赋予了13个methods来操作关联的各类动作。如build ,create等等。
例子:
class Customer < ActiveRecord::Base
has_many :orders
end
collection(force_reload = false) 实例:@orders = @customer.orders
collection<<(object,...) 实例: @customer.orders << @order1
解释:增加一个或多个order对象. 如果用delete移除了某个对象,可以使用这个method增加回来,自动为这个对象的外键赋值,nil不再为空。
collection.delete(object...) 实例:@customer.orders.delete(@order1)
//简单解释:不删除只去掉关联
解释:delete,从@customer.orders移除一个或多个object,方法是通过把@order1的关联外键设为nil.这样再使用@customer.orders的时候,就不会调用已经移除的@order1,但Order数据库中仍然存在@order1.
collection.clear,是移除全部,和delete类似。
collection.empty? 解释:如果是空的则返回true
collection.exists?(...) 解释:根据(...)条件返回true或false,如果不加(...)根据是否有关联对象返回boolean值。
colleciton.size 解释:returns the number of objects in the collection.
colleciton.find(...) 实例:
@open_orders = @customer.orders.find(:all, :conditions => "open = 1")
解释:不明白怎么查找的???
4.3.2 Options for has_many
You can alter that behavior in a number of ways.For example, an association with several options might look like this:
class Customer < ActiveRecord::Base
has_many :orders, :dependent => :delete_all,
:validate => :false
end
合计有22个选项options可进行customization.
常用举例:
:validate, 如果为false,保存customer对象的时候,不验证关联的orders对象。
:as, 设置别名,或者使用多态关联(还没有体会到方便的用途),见2.9 Polymorphic Associations
:autosave, 当保存父对象时,自动保存所以子对象,并把标记为destruction的删除
:class_name, 举例:如果一个customer有很多orders,但是实际包含orders的model的名字是Transaction,需要如下写法:
class Customer < ActiveRecord::Base has_many :orders, :class_name => "Transaction"
end
:source, 🈯️定has_many :through 即多对多关联的关联源名称,只有无法从关联名字中解出源关联名的时候才会用到source这个选项。和class_name的用途一样,但作用范围有区别。
:through, 用于建立多对多关联的方法之一。
:conditions, 进行条件筛选。关联的对象,必须满足相应条件
:dependent => destroy, 删除关联的子对象,并在删除前recall子对象。
:dependent => delete_all, 直接删除。
:dependent => nullify, 设置子对象外键为null,不再关联。
:foreign_key, by convention, Rails guesses that the column used to hold the foreign key on the other model is the name of this model with the _id added. 我的理解就是不按照rails约定的习惯,设定一个不是model名字的XX_id列,然后在has_many :orders, :foreign_key => "XX_id",🈯️定这个column为外键。
例子:class Customer < ActiveRecord::Base
has_many :orders, :foreign_key => "cust_id"
end
:include, 用于嵌套的model关联,比如一个customer有很多orders,一个orders有很多lineitems,用:include声明后,可以直接使用@customer.orders.lineitems
:limit, 用于限定fetch的子对象的数量,例子:
class Customer < ActiveRecord::Base
has_many :recent_orders, :class_name => "Order", :order => "order_date DESC", :limit => 100
end
:order, 用于规定关联的子对象的排列顺序。
:offset, 例子: :offset =>11, 调用的时候忽略第11个record