疑问:
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。
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.
Post.any?
Post.many?
Post.recent.any?
Post.recent.many?
Post.where(
:published
=>
true
).any?
Post.where(
:published
=>
true
).many?
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学习的网址 收集了常用RAILS学习的网址
Ruby on Rails 4.0 Guide 英文mobi 本资源转载自网络,如有侵权,请联系上传者或csdn删除 查看此书详细信息请在美国亚马逊官网搜索此书
《ruby on rails web开发学习实录(配光盘)》基于ruby 1.9.2和rails 3.0.3全面探讨ruby on rails框架的组成部分。全书共分17章,主要内容包括:搭建ruby on rails开发平台、ruby语言的基础知识(像控制语句、面向对象...
Ruby on Rails Guide:是rails官方教程,本人为了大家学习查阅的方便,制成chm格式。就如同java doc的chm格式一样方便。
rails指南 中文版
主要介绍ruby的开发框架ruby 的一些目录结构和框架啊源代码的应用,
Ruby on Rails心得_V0.1 Ruby on Rails心得_V0.1
rails学习教程,ruby on rails入门,pdf格式,英文
introduction for rails 4 development, it is very good document.
Ruby On Rails是一个用于编写网络应用程序的软件包.它基于一种计算机软件语言Ruby,给程序开发人员提供了强大的框架支持.本书介绍了rails的基本使用,深入扩展,练习挺多的
学习Ruby on Rails 4.0的逐步指南。 它包括针对Ruby 2.0.0的基本教程,是为至少了解另一种编程语言并熟悉HTML的程序员编写的。
rails-beginner-s-guide是Rails 指导手册,帮组学习了解rails开发
《Ruby on rails web开发学习实录》的光盘文件
sunspot 学习笔记-安装以及使用,另外还有部分crontab插件的安装以及使用,适用与rails3
Ruby on Rails Web开发学习实录.pdf
Ruby on Rails Guides_ A Guide to Testing Rails Applications.pdf
NULL 博文链接:https://citi21558541.iteye.com/blog/437132
很不错的入门文档,适合初学者直接入门。
仅仅三分,就可以帮助你搭好框架,拥有学习手册。欢迎下载其他文档