2010年11月18日 星期四

render template

render syntax
1.render action
render(:action=>"hello")

2.render text
render(:text=>"welcome to use ruby on rails language")

3.render action
render(:template=>"demo/index")

create one to many relationship

1.use has_one create connection with page on subject
#vim app/models/subject.rb

add new line in it
has_many :pages

2.use belongs_to create connection with subject on page
# vim app/models/page.rb

add two line in it
belongs_to :subject
has_many: sections

3.use belongs_to to create connection with page on section
add new line in it
belongs_to :page

3.use console create connection on subject with page
#rails console
subject = Subject.find(2)
page = Page.first
subject.pages << page

4.use size to get the account for subject
subject.pages.size
subject.pages.empty?
subject.pages.delete(page)
subject.pages[1]

create one to one relationship

1.use has_one create connection with page on subject
#vim app/models/subject.rb

add new line in it
has_one :page

2.use belongs_to create connection with subject on page
# vim app/models/page.rb

add new line in it
belongs_to :subject

3.use console create connection on subject with page
#rails console
first_page = Page.new(:name=>"first page",:permalink=>"first",:position=>1)
first_page.save

second_page = Page.new(:name=>"second page",:permalink=>"second",:position=>1)
second_page.save

4.use subject to get id 2 on Subject
subject = Subject.find_by_id(2)
subject.page = second_page

2010年11月17日 星期三

find the record

1.use rails console to enter the console
#rails console

2.find the record, find, find_by_id, find_by_name
#subject = Subject.find(2)
#subject = Subject.find_by_id(2)
#subject = Subject.find_by_name("first subject")

3.find all record
#Subject.all

4.find first or last record
#Subject.first
#Subject.last

variable scope

local variable
$global variable
@instance variable
@@class variable
CONSTANT
:symbol
ClassName

2010年11月16日 星期二

find,update and delete one record

1.use rails console
#rails console

2.use subject.find
#subject=Subject.find(2)

3.update attribute
#subject.name="twenty man"
#subject.save
or you can use update_attribute
#subject.update_attributes(:name=>"powerman",:position=>2)

4.destory one object
#subject.delete
#subject.frozen?

create new record

1.use console
#rails console

2.create new subject instance
subject = new Subject
subject.new_record?
subject = Subject.new(:name=>"first man",:position=>2,:visible=>true)
subject.save

3.create sceond subject instance
subject = Subject.create(:name=>"second man",:position=>1,:visible=>true)
subject.save

4.use mysql command to confirm this
#mysql -u root -p
use cms_development
select * from Subjects;