Difference between revisions of "Rails3"
 (→More info)  | 
				 (→More info)  | 
				||
| (4 intermediate revisions by the same user not shown) | |||
| Line 95: | Line 95: | ||
 rake db:drop db:create db:migrate  | 
   rake db:drop db:create db:migrate  | 
||
== Cardinality and associations ==  | 
|||
Active Record associations can be used to describe one-to-one, one-to-many and many-to-many relationships between models. Each model uses an association to describe its role in the relation.   | 
|||
The '''belongs_to''' association is always used in the model that has the '''foreign key'''.  | 
|||
==== one-to-one ====  | 
|||
 class Employee < ActiveRecord::Base  | 
|||
   '''has_one''' :office  | 
|||
 end  | 
|||
 class Office < ActiveRecord::Base  | 
|||
   '''belongs_to''' :employee    # foreign key - employee_id  | 
|||
 end  | 
|||
==== many-to-one ====  | 
|||
 class Manager < ActiveRecord::Base  | 
|||
   '''has_many''' :employees  | 
|||
 end  | 
|||
 class Employee < ActiveRecord::Base  | 
|||
   '''belongs_to''' :manager     # foreign key - manager_id  | 
|||
 end  | 
|||
==== many-to-many ====  | 
|||
Method 1. With an intermediate table (Assignment)  | 
|||
 class Assignment < ActiveRecord::Base  | 
|||
   '''belongs_to''' :programmer  # foreign key - programmer_id  | 
|||
   '''belongs_to''' :project     # foreign key - project_id  | 
|||
 end  | 
|||
 class Programmer < ActiveRecord::Base  | 
|||
   '''has_many''' :assignments  | 
|||
   '''has_many''' :projects, ''':through''' => :assignments  | 
|||
 end  | 
|||
 class Project < ActiveRecord::Base  | 
|||
   '''has_many''' :assignments  | 
|||
   '''has_many''' :programmers, ''':through''' => :assignments  | 
|||
 end  | 
|||
Method 2. With an intermediate table that has neither model nor primary key  | 
|||
 class Programmer < ActiveRecord::Base  | 
|||
   '''has_and_belongs_to_many''' :projects       # foreign keys in the join table  | 
|||
 end  | 
|||
 class Project < ActiveRecord::Base  | 
|||
   '''has_and_belongs_to_many''' :programmers    # foreign keys in the join table  | 
|||
 end  | 
|||
----  | 
|||
[http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html More here]  | 
|||
== Database field types ==  | 
  == Database field types ==  | 
||
| Line 123: | Line 176: | ||
| :timestamp || datetime  | 
  | :timestamp || datetime  | 
||
|}  | 
  |}  | 
||
= UTF-8 =  | 
  = UTF-8 =  | 
||
| Line 138: | Line 189: | ||
= More info =  | 
  = More info =  | 
||
| ⚫ | |||
* [http://guides.rubyonrails.org/ Guides at RubyonRails.org]  | 
|||
* [http://railscasts.com/ RailsCasts] tutorials  | 
|||
| ⚫ | |||
See also [[RVM]]  | 
  See also [[RVM]]  | 
||
Latest revision as of 20:37, 9 July 2012
Developing for Rails3.
Create a new application
Test your installation by making a test application
rails new tetsApp cd testApp rails server
Now check the http://localhost:3000 in your browser.
Nonstop first app
This example is adapted from here
rails new blog cd blog bundle install
gedit config/database.yml rake db:create
rails generate controller home index gedit app/views/home/index.html.erb
Enter the new web page content:
<h1>Hi there!</h1>
Set up the new root page
rm public/index.html gedit config/routes.rb
Edit routes, like this
... root :to => "home#index" ...
Test your server at http://localhost:3000
rails server
Create scaffolding
rails generate scaffold Post name:string title:string content:text rake db:migrate
Database
Using Mysql
Making a new application
database.yml file
development:
adapter: mysql2 encoding: utf8 database: blog_development pool: 5 username: root<change this> password: <change this> socket: /tmp/mysql.sock
Using Postgres
Make sure you have pg gem installed (not the old "postgres" gem)
gem install pg
Making a new application
rails -d postgresql my_rails_app
database.yml file
development: adapter: postgresql host: localhost port: 5432 username: your_db_username password: your_db_password database: your_rails_project_development schema_search_path: public encoding: utf8 template: template0
Reset database
Reset database contents
rake db:reset rake db:migrate
or
rake db:reset db:migrate
Recreate database structure
rake db:drop rake db:create rake db:migrate
or
rake db:drop db:create db:migrate
Cardinality and associations
Active Record associations can be used to describe one-to-one, one-to-many and many-to-many relationships between models. Each model uses an association to describe its role in the relation.
The belongs_to association is always used in the model that has the foreign key.
one-to-one
class Employee < ActiveRecord::Base has_one :office end class Office < ActiveRecord::Base belongs_to :employee # foreign key - employee_id end
many-to-one
class Manager < ActiveRecord::Base has_many :employees end class Employee < ActiveRecord::Base belongs_to :manager # foreign key - manager_id end
many-to-many
Method 1. With an intermediate table (Assignment)
class Assignment < ActiveRecord::Base belongs_to :programmer # foreign key - programmer_id belongs_to :project # foreign key - project_id end class Programmer < ActiveRecord::Base has_many :assignments has_many :projects, :through => :assignments end class Project < ActiveRecord::Base has_many :assignments has_many :programmers, :through => :assignments end
Method 2. With an intermediate table that has neither model nor primary key
class Programmer < ActiveRecord::Base has_and_belongs_to_many :projects # foreign keys in the join table end class Project < ActiveRecord::Base has_and_belongs_to_many :programmers # foreign keys in the join table end
Database field types
| Rails Migration Symbol | MySQL Data Type | 
|---|---|
| :binary | blob | 
| :boolean | tinyint(1) | 
| :date | date | 
| :datetime | datetime | 
| :decimal | decimal | 
| :float | float | 
| :integer | int(11) | 
| :string | varchar(255) | 
| :text | text | 
| :time | time | 
| :timestamp | datetime | 
UTF-8
If you need UTF-8 encoding for your text in program files, add this magic comment to your file:
# encoding: utf-8
If you need UTF-8 in your (mysql) database(s), add "encoding: utf8" in the config/database.yml
adapter: mysql ... encoding: utf8
More info
- RailsCasts tutorials
 
See also RVM