Rails Guide学习心得和疑问

阅读更多

疑问:

has_and_belongs_to_many :hobbies 关联表按照guides说明,我将其命名为users_hobbies,但是它却关联hobbies_users。我不服,就改成了has_many through的方式了。

The build_association  method returns a new object of the associated type. This object will be instantiated from the passed attributes, and the link through this object’s foreign key will be set, but the associated object will not  yet be saved.

@customer = @order .build_customer( :customer_number => 123 ,
   :customer_name => "John Doe"

 

没保存的话何来id呢?我只能理解为虽然build_customer还没有被保存,但是在未来,它会被保存。

 

4.1.2.2 :class_name

If the name of the other model cannot be derived from the association name, you can use the:class_name  option to supply the model name. For example, if an order belongs to a customer, but the actual name of the model containing customers is Patron , you’d set things up this way:

class Order < ActiveRecord::Base
   belongs_to :customer , :class_name => "Patron"

 

我理解为如果在Order中无法获得customer,而只能通过Patron来获得customer,则用此写法。

:group 是什么意思?

 

  • proxy_owner  returns the object that the association is a part of.
  • proxy_reflection  returns the reflection object that describes the association.
  • proxy_target  returns the associated object for belongs_to  or has_one , or the collection of associated objects for has_many  or has_and_belongs_to_many .

 

上面是什么意思?

 

Joining Nested Associations (Multiple Level)
Category.joins( :posts => [{ :comments => :guest }, :tags ])

 

我理解上面的含义是关联到的comments是传入了:guest值参数匹配到的,posts就同理了,全部Inner join。

 

 

Client.exists?

The above returns false  if the clients  table is empty and true  otherwise.

You can also use any?  and many?  to check for existence on a model or relation.

# via a model
Post.any?
Post.many?
 
# via a named scope
Post.recent.any?
Post.recent.many?
 
# via a relation
Post.where( :published => true ).any?
Post.where( :published => true ).many?
 
# via an association
Post.first.categories.any?
Post.first.categories.many?

 

谁能告诉我any? many? exists?的区别。

Client.count(:age)表达的是什么意思?

 

<%= render :partial => f %>是什么意思?
3.4.7 Spacer Templates
<%= render @products , :spacer_template => "product_ruler" %>

Rails will render the _product_ruler partial (with no data passed in to it) between each pair of _product partials.

我理解上文的含义是这个模块是对于一些重复代码的自动调用。因为传入的是一个connection,有循环调用,不涉及变量的一些html可以用_product_ruler封装起来。

 

around_filter 没看明白。

routing.html

 

resource :geocoder如何与前面关联起来的?


 

 

 

Migrate

rollback回滚的是最近的一次migrate

rails generate AddXddToPost 这样的写法是会创建日期_add_xdd_to_post.rb这样的文件,就是根据开头大写的字母替换为_小写字母

t.references :categor y这样的写法并不会真正的创建外键,只是创建了category_id这个int的字段而

已,并不检验category这个东西是不是个东西,只是方便自己理解和写而已。

rake db:migrate:up VERSION=20080906120000 只是执行 VERSION=20080906120000的up

方法,而去掉:up ,则会将数据库结构执行到VERSION=20080906120000,可能执行多个.rb

下面表示可以验证符合字段的唯一性:

class Holiday < ActiveRecord::Base
   validates_uniqueness_of :name , :scope => :year ,
     :message => "should happen once per year"
end

 

class Order < ActiveRecord::Base
   belongs_to :customer
end

 

这样定以后,会生成几个方法,然后通过生成的方法使得我们可以如此引用它:

@customer = @order .customer

@order .customer = @customer

@customer = @order .build_customer( :customer_number => 123 ,

   :customer_name => "John Doe" )

@customer = @order .create_customer( :customer_number => 123 ,
   :customer_name => "John Doe" )

 

两者区别在于后者custormer会被保存。

 

注意最好不要修改migration的time文件名,修改文件内容后,可以用rollback,或者rake db:migrate:up/down VERSION=xxx的方式更新。文档推荐增量,说这样安全无害些,缺点就是文件太多了,乱。

 

修改application.rb下的config.i18n.default_locale = :zh需要重启web才能生效。

 

你可能感兴趣的:(Rails Guide学习心得和疑问)