แสดงบทความที่มีป้ายกำกับ rails3 แสดงบทความทั้งหมด
แสดงบทความที่มีป้ายกำกับ rails3 แสดงบทความทั้งหมด

วันอังคารที่ 3 เมษายน พ.ศ. 2555

rails 3: Rails logger outside controller and model

ทำอย่างไร ถ้าต้องการเขียน log นอก controller และ model ?


ใช้ Rails.logger.info "Some debugging info I want to see in my development log."

วันพุธที่ 28 มีนาคม พ.ศ. 2555

Rails3: How to config rails project no database ?

มีวิธีทำให้ Rails Project ไม่ต้องใช้ database อย่างไร

ใน config/application.rb ให้ comment #require 'rails/all'

ใน config/environments/development.rb ให้ comment #config.active_record.xxxxxxx...

และให้เพิ่ม code ข้างล่างนี้แทนที่
require "action_controller/railtie"
require
"action_mailer/railtie"
require
"active_resource/railtie"
require
"rails/test_unit/railtie"
require
"sprockets/railtie"
อ้างอิง : http://stackoverflow.com/questions/3954307/rails-3-how-do-i-avoid-database-altogether


วันอังคารที่ 27 มีนาคม พ.ศ. 2555

Rails : Formatting a date in a SQL query in Rails

ต้องการส่งวันที่และเวลา เข้าไปใน SQL Query ?

User.where("created_at < ?", Time.now.to_formatted_s(:db))

วันอาทิตย์ที่ 28 สิงหาคม พ.ศ. 2554

rails 3: Rails Footnotes

1. gem 'rails-footnotes', '>= 3.7', :group => :development
2. create footnotes.rb in initializers
3. insert code :
if defined?(Footnotes) && Rails.env.development?
Footnotes.run! # first of all

# ... other init code
Footnotes::Filter.prefix = 'mvim://open?url=file://%s&line=%d&column=%d'
end

วันพฤหัสบดีที่ 18 สิงหาคม พ.ศ. 2554

rails 3: query_reviewer

  1. Gemfile - gem "query_reviewer", :git => "git://github.com/nesquena/query_reviewer.git"
  2. Terminal - Bundle install
  3. Terminal - rake query_reviewer:setup
  4. View - add <%=query_review_output%> in xxxxx.erb file
  5. See result following picture above

วันอังคารที่ 28 มิถุนายน พ.ศ. 2554

Rails 3: load schema to production environment

  1. Development Environment : rake db:schema:dump
  2. Production Environment : rake db:schema:load RAILS_ENV=production

วันศุกร์ที่ 1 เมษายน พ.ศ. 2554

rails 3: foreign key in migration

class ExampleMigration < ActiveRecord::Migration def self.up
create_table :products do |t|
t.references :category
end
#add a foreign key
execute <<-SQL ALTER TABLE products ADD CONSTRAINT fk_products_categories FOREIGN KEY (category_id) REFERENCES categories(id) SQL add_column :users, :home_page_url, :string rename_column :users, :email, :email_address
end

def self.down
rename_column :users, :email_address, :email
remove_column :users, :home_page_url
execute "ALTER TABLE products DROP FOREIGN KEY fk_products_categories"
drop_table :products
end

rails 3 : rake command on environment .

ใช้ parameter ที่ชื่อว่า RAILS_ENV ส่งค่าตาม environment ที่ต้องการตามตัวอย่างนี้

rake db:create RAILS_ENV=test
rake db:migrate RAILS_ENV=test
rake db:drop RAILS_ENV=test

วันจันทร์ที่ 14 กุมภาพันธ์ พ.ศ. 2554

rails 3 : Configuration for PostgreSQL


  1. ติดตั้ง PostgreSQL ใน Rails 3 โดยไปเพิ่มสองบรรทัดใน Gemfile....
    • source 'http://gemcutter.org'
    • gem 'pg', '0.9.0'
  2. หลังจากนั้นใช้คำสั่ง > bundle install
  3. ใส่ config ใน database.yml ประมาณนี้
  4. development:
    adapter: postgresql
    host: localhost
    port: 5432
    username: postgres
    password: xxxxx
    database: pg_development
    schema_search_path: test
    encoding: utf8
    template: template0
  5. ทดสอบ เข้า irb โดยใช้คำสั่ง > rails console