2007年12月17日 星期一

外鍵, :joins, :conditions(2) :include

@tasks = Task.find(:all,
                              :include => :goal 
                              :conditions => "goals.name = 'abc' ")

Rails 提供了:include 這樣的選擇, 使用:include選項時在其他參數(如conditions)用到欄位名稱(如name)時,就要加上table name(goals)這樣它才知道正確的查詢

2007年12月12日 星期三

外鍵, :joins, :conditions

JOIN的一些說明
SQL JOIN
left join/right join/inner join的用法比較

example:

goals table

id name
1 abc
2 def

tasks table

id name goals_id
1 task1 1
2 task2 1
3 task3 2

 

想用goal name 做查詢時


@tasks = Task.find(:all,
                              :joins => "inner join goals on tasks.goal_id = goals.id",
                              :conditions => "goals.name = 'abc' ",
                              :select=>"tasks.id, tasks.name, task.goals_id")

如果沒有用:select 會find出

id name goals_id id name
1 task1 1 1 abc
2 task2 1 1 abc

這樣如果直接查@tasks.id 都是查到後面的 goal的id 都是1

所以要用:select 去取出tasks table的部份

這樣便可以查出

id name goals_id
1 task1 1
2 task2 1


2007年12月5日 星期三

取得 select_date 所設定的值, 範圍 prefix

View
<% form_for :task ,:url => {:action => 'query_date_tasks' } do |form|%>
 
 <%= select_date Date.today, :prefix => "stay_from", :include_blank => true %>


<%= select_date Date.today, :prefix => "stay_to", :include_blank => true %>
<%= submit_tag "submit" %>
<% end %>

:prefix 用來設定參數的名稱
:params => { :stay_from => { :year => 2007, :month => 12, :day => 5}}


Controller

def query_date_tasks
 date_stay_from = Date.new(params[:stay_from][:year].to_i,
               params[:stay_from][:month].to_i,
               params[:stay_from][:day].to_i)

 date_stay_to = Date.new(params[:stay_to][:year].to_i,
             params[:stay_to][:month].to_i,
             params[:stay_to][:day].to_i)

 @tasks = Task.find(:all, :conditions => ["finish_time BETWEEN ? AND ?", date_stay_from, date_stay_to])

 render :action => 'list_all_tasks'
end

sortable column headers 使用

Controller
def list_all_tasks
 add_to_sortable_columns('listing', Task, 'task_name')
 add_to_sortable_columns('listing', Task, 'finish_time')
 add_to_sortable_columns('listing', Goal, 'name')

 @tasks = Task.find(:all, :include => :goal, :order => sortable_order('listing', Task, 'task_name'))
end

View
<%= link_to '任務名稱', sort_param('listing', Task, 'task_name') %>
<%= link_to '任務完成時間', sort_param('listing', Task, 'finish_time') %>
<%= link_to '任務所屬目標', sort_param('listing', Goal, 'name') %>

add_to_sortable_columns 是加在需要的controller's action

sortable column headers 使用

Model

def list_all_tasks
add_to_sortable_columns('listing', Task, 'task_name')
add_to_sortable_columns('listing', Task, 'finish_time')
add_to_sortable_columns('listing', Goal, 'name')
@tasks = Task.find(:all, :include => :goal, :order => sortable_order('listing', Task, 'task_name'))
end

View

<%= link_to '任務名稱', sort_param('listing', Task, 'task_name') %>
<%= link_to '任務完成時間', sort_param('listing', Task, 'finish_time') %>
<%= link_to '任務所屬目標', sort_param('listing', Goal, 'name') %>



add_to_sortable_columns 柿子  

2007年12月4日 星期二

sortable column headers 安裝

安裝sortable column headers

基本上, 這樣應該是可以裝的
ruby script/plugin install https://svn.elctech.com/svn/public/sortable_column_headers/

不過作者說了
whoever is maintaining our SVN repository is trying to do something
clever with HTTPS and it isn't working 100% yet :) Try grabbing it
directly from our repository into your Rails app with Subversion like
this (you should execute this command from within your Rails app's root
directory): svn export https://svn.elctech.com/svn/public/sortable_column_headers ./vendor/plugins/sortable_column_headers

所以我們得先安裝SVN才行, 這時可以參可這篇如何在Windows Console下使用命令svn

手動或者綠色安裝svn命令軟件Subversion:

下載軟件版本1.4.3:http://subversion.tigris.org/downloads/svn-win32-1.4.3.zip

安裝方法:7z x svn-win32-1.4.3.zip
mv svn-win32-1.4.3 c:\programme\.
SET SVN_HOME=c:\programme\svn-win32-1.4.3
SET PATH=%PATH%;%SVN_HOME%\bin

這時便可以到你的app(rails_apps\YourApp)下執行

svn export https://svn.elctech.com/svn/public/sortable_column_headers ./vendor/plugins/sortable_column_headers




順邊參考如何安裝Plugin/Engines

2007年12月1日 星期六

如何提交多個check box

list.rhtml

口 AAA_checkbox
口 BBB_checkbox
[submit]

我想要在按下submit之後, 可以更新check box裡面object的某些值後回到list

-------------------
Model

class Goal < style="font-weight: bold;">View, list.rhtml


<% if task.do_or_not? %>
 <%= check_box_tag "task["+task.id.to_s+"][checked]","1",:checked =>"checked"%>
<% else %>
 <%= check_box_tag "task["+task.id.to_s+"][checked]","1"%>
<% end %>
<%= hidden_field_tag("task["+task.id.to_s+"][checked]","0") %>

<%= h(task.task_name) %>

-----------------------
從上面的view 我們可以得到這樣的params

params: {
 :controller=>"user",
 :task=> {
  "2"=>{"checked"=>"0"},
  ... etc...
  "4"=>{"checked"=>"1"}
 },
 :action=>"set_auto_check"
}

---------------------
Controller
, user_controler.rb

def set_auto_check
 @params[:task].each { |task_id, attr|
 task = Task.find(task_id)
 task.update_attribute(:checked, attr[:checked])
}

redirect_to :action => 'list'
end

-------------------------
而controller裡會把 "2"放到 task_id, "checked" =>"0" 放到 attr.

這樣便可以完成提交多個check box

2007年11月27日 星期二

Globalize plugin

Get on Rails wit Globalize

修改environment.rb 記得要

restart application


不是 web server

2007年11月16日 星期五

REST

參考了許多人家的理解
REST簡介
DHH的演講翻譯
學習REST

目前自己的理解是 Web上的東西都是Resource, Resource 對HTTP的不同動作(CRUD -- Create,Read,Update和Delete), 該有不同的反應

2007年11月13日 星期二

h() , Time.now() and link_to()

h() method prevents the special characters in the e-mail address or ...

Time.now()
@time = Time.now
<%= @time %>

link_to()
<%= link_to "Goodbye", :action => "goodbye" %>

---------------------------------------------------------------------------
In controller:
@files = Dir.glob('*' )

In view:
<% for file in @files %>
 file name is <%= file %>
<% end %>

2007年11月12日 星期一

<%= ... %> , <% ... %> and <% ... -%>

<%= ... %> 內容會輸出
<% ... %> 內容不會輸出
<% ... -%> -號會把預設的空白行去掉

在rails 1.2.3 在ruby 1.8.6測試已經都不會有預設的空白行了

Sample1
<% 3.times do -%>
hi
<% end -%>
over

hi
hi
hi
over

Sample2
<% 3.downto(1) do |count| -%>
 <%= count %> ... <br/> <br/>才有真正換行 <%=%> 沒有
<% end -%>
kick off

3 ...
2 ...
1 ...
kick off

2007年11月11日 星期日

Controller View and URL

禮拜日好懶 ==
demo>ruby script/generate controller Say

2007年11月10日 星期六

Installing Rails

在windows 直接用installRails安裝

查詢版本
ruby -v, rails -v


更新版本
C:\rails_apps> gem update rails --include-dependencies
C:\rails_apps> gem cleanup
app> rake rails:update


Rails和database的階層關係
  • Ruby Programming
  • Ruby database adapter , Each database library have its own database-specific API, Ruby database adapter hide these differences
  • Database driver , Ruby library connect low-level database API to the high-level world of ruby programming
  • Database engine

2007年11月9日 星期五

Action Pack:The View and Controller

The controller supplies data to the view, and the controller receives back events from the pages generated by the views
The interactions , support for views and controllers in Rails is bundled into a single component , Action Pack


View Support
view 中的一些動態內容 大都是由controller的action由model取來的資料運用產生的
而Rails支援動態產生內容的格式有 rhtml, rxml, rjs ,這些格式在Rails被稱為template
  • rhtml:利用ERB的Ruby工具(embedded ruby)將 Ruby的code嵌入HTML的code中
  • rxml :可用Ruby的code建立XML文件, 產生的XML會自動符合code架構
  • rjs :可在Server端建立Javascript,在browser端執行 ,也就是用來做Ajax的

Controller
Controller是全部的邏輯中心, control user, view and model 之間的互動, 該去哪該做啥,做完給誰 ,而底層的互動已經被Rails做掉了


Controller除了之間的互動 還有
  • 把外部的request , URL指引到內部的action
  • cache : for performance
  • helper module for 擴增view的功能 ,又讓view本身的程式碼不會變大,就是可以多一堆plug-in的function的感覺
  • session : giving users the impression of an ongoing interaction with our applications???

2007年11月8日 星期四

Active Record: Rails Model Support

Object/Relational Mapping
ORM 把datebase table mapping 到class。如果有database table叫orders, 那就有相對應個class叫做Order. tables correspond to classes. rows in a table correspond to objects of the class


  • 透過ORM,把database整個物件化,讓我們不用直接存取 database data

example:
order = Order.find(1)
puts "Order #{order.custom_id}, amount=#{order.amount}"

Order.find(:all, :conditions => "name='lake'") do |order|
 puts order.amount
 order.discount = 0.5
 order.save
end


  • ActiveRecord就是Rails用的ORM層
example:
require 'active_record'
class Order <>

2007年11月7日 星期三

Models, Views, and Controllers

  • modal 用來存資料的 ,長時間的資料庫,短時間的cache都是存這,除了單純放資料外,還可以對資料加上基本的存取規則,避免程式上邏輯錯誤造成的資料存取。
  • view 顯示使用者介面,透過model可讀取資料,讀出的資料,靠view顯示,也只負責顯示,所以有可能好幾個view同時存取同一個model在不同情況下讀取相同資料做不同的顯示
  • controller 接收使用者指令 , 選擇對應的method給model 再把相對應的資料給view


  • Rails 強迫把程式分成controller,view,and model三個部份,然後再照Rails預設規則去跑.這就是應該所謂的 convention over configuration

2007年11月5日 星期一

NetBeans 的使用

我有用的快捷鍵
雖然官方的NetBeans講了很多快捷鍵的功能, 可是我只有試出幾種, 可能也是我沒仔細去看吧
  • Ctrl + Alt +Space 或 Ctrl + \ : Auto Complete
  • Ctrl + Tab : Change Tab
  • Alt + Enter : How to use Surround With
  • Ctrl + mouse Left Key 去點method 會跳到去宣告的地方
  • Tools -> Options -> Keymap 可以設定自己習慣的快捷鍵
  • re + tab:<%=%>
  • r + tab:<%%>
  • ctrl + g:快速定位
  • ctrl + k:自動完成剩下的程式碼

如何讓NetBeans的debug 功能可以用

1. D:\InstallRails\rails_apps>gem install ruby-debug-ide

2. Change tool -> Options -> Ruby interpreter to D:\InstantRails\ruby\bin\ruby.exe