2009�N12��15��

blank? nil? zero?

nil? zero? blank? �̓����̈Ⴂ�B

0.nil? #=> false
0.zero? #= true
0.empty? #=> NoMethodError
0.blank? #=> false
0 == false #=> false

"".nil? #=> false
"".zero? #=> NoMethodError
"".empty? #=> true
"".blank? #=> true
"" == false #=> false

{}.nil? #=> false
{}.zero? #=> NoMethodError
{}.empty? #=> true
{}.blank? #=> true
{} == false #=> false

[].nil? #=> false
[].zero? #=> NoMethodError
[].empty? #=> true
[].blank? #=> true
[] == false #=> false

nil.nil? #=> true
nil.zero? #=> NoMethodError
nil.empty? #=> NoMethodError
nil.blank? #=> true
nil == false #=> false

false.nil? #=> false
false.zero? #=> NoMethodError
false.empty? #=> NoMethodError
false.blank? #=> true
false == false #=> true
posted by ������ at 15:12| Comment(77) | TrackBack(0) | ��{ | ̃uO̓ǎ҂ɂȂ | XV`FbN

Ruby�̉��Z�q�̗D�揇��

Ruby�ɂ� AND,OR ��\�����Z�q��2��ނ���܂��B

&& ||
and or

�ł����A�ǂ�����g�������������̂��A�Ƃ����c�_������܂����B
�l�� and,or �h�ŁA����͉��Z�q�̗D�揇�ʂ��Ⴂ����B
���ہA���܂���ɂȂ鎖�͖����̂ł����A���̂悤�ȃP�[�X�ł͌��ʂ�����Ă��܂��B

i = 0; i = 1 and i == 1
#=> true
i
#=> 1

i = 0; i = 1 && i == 1
#=> false
i
#=> false

�D�揇�ʂ�������悤�Ɋ��ʂ��‚���ƁB

i = 0; (i = 1) and (i == 1)
i = 0; i = (1 && (i == 1))

��̏ꍇ�́Ai�ɂ�1���͂�܂����A���̏ꍇ�ɂ́Ai��false������܂��B
posted by ������ at 12:05| Comment(41) | TrackBack(0) | ��{ | ̃uO̓ǎ҂ɂȂ | XV`FbN

Rails�ŃC���X�^���X�̃R�s�[

Rails�ŃC���X�^���X�̃R�s�[����肽���Ƃ��ɁA�ז��ɂȂ�̂�
attr_protected
���ꂪ����ƁAattributes= �ŃR�s�[����Ȃ��������o�Ă���B
�Ƃ͌����A�Z�L�����e�B�[���l����ƁAattr_protected���w�肵�Ȃ����͍l�����Ȃ��̂ŁA�C���X�^���X�̃R�s�[���������ꍇ�͎��̂悤�ɍs���B


class User < ActiveRecord::Base
attr_protected :is_admin
end
user = User.new
user.attributes = {:username=>'Phusion', :is_admin=>true}
user.username # => "Phusion"
user.is_admin? # => false

user.send(:attributes=, {:username=>'Phusion',:is_admin=>true}, false)
user.is_admin? # => true

posted by ������ at 09:37| Comment(26) | TrackBack(0) | ��{ | ̃uO̓ǎ҂ɂȂ | XV`FbN

2009�N09��08��

�T�u�N�G�����܂� Condition �̍���

�v���O�C�� condition builder �ɃT�u�N�G������������@�B
�Ō�� and ���\�b�h�́usql�v�w��ł͑�Q���������̂܂� and �Ōq����B

cnd = Condition.new
cnd.and('items.id', [1,2,3,4])
cnd.and do |c|
�@c.or('items.start_at', 'is', nil)
�@c.or('items.start_at', '<=', Time.now)
end
cnd.and('sql', 'exists (select * from liquors where iquors.item_id = items.id)')

Item.find(:all, :conditions=>cnd.where)
�����s����Ǝ���SQL�������s����܂��B

SELECT * FROM items
WHERE (items.id IN (1,2,3,4)
�@AND (items.start_at is NULL OR items.start_at <= '2009-09-08 10:54:42')
�@AND exists (select * from liquors where iquors.item_id = items.id))

posted by ���񂢂����� at 10:58| Comment(50) | TrackBack(0) | �v���O�C�� | ̃uO̓ǎ҂ɂȂ | XV`FbN

2009�N06��26��

Ruby Enterprise Edition + Passenger + mod_rewrite

Ruby Enterprise Edition + Passenger �𓱓����Ă݂܂����B

�C���X�g�[�����@��A���ʂɂ‚��Ă͑�R�̕������y���Ă���悤�Ȃ̂Ŋ��������Ă��炢�܂��B

�����‚���肪���������̂őΉ�����B

��connection error ������

�m�F�̂��߃T�C�g�����񂵂Ă���ƁA���񂩂Ɉ��
Mysql::Error: Lost connection to MySQL server during query
�Ƃ����G���[���o���B

gem install mysql

�ʼn���

��mod_rewrite�Ƃ̕��p

�������̂́Amod_rewrite�Ɋւ��āB

virtualhost�Ɉȉ���ݒ肵�ē���������ł����Arewrite�������ĂȂ��͗l�B�B�B


PassengerHighPerformance on
PassengerMaxPoolSize 30
PassengerMaxInstancesPerApp 0
PassengerPoolIdleTime 300
PassengerMaxRequests 0

<VirtualHost *:80>
ServerName rails.sample.jp
DocumentRoot /path-to-rails/public
<Directory "/path-to-rails/public">
Options -Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
Allow from all
</Directory>

RailsAllowModRewrite on

RewriteEngine On

RewriteCond %{DOCUMENT_ROOT}/maintenance.html -f
RewriteCond %{REMOTE_ADDR} !^192\.168\.
RewriteRule . /maintenance.html [L]

RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
RewriteRule ^/(.*)$ /%{REQUEST_URI} [P,QSA,L]

ProxyRequests Off
ErrorLog "|/usr/sbin/rotatelogs /var/log/apache2/rails.sample.jp_error.log.%Y%m%d 86400 540"
LogLevel warn
CustomLog "|/usr/sbin/rotatelogs /var/log/apache2/rails.sample.jp_access.log.%Y%m%d 86400 540" combined
ServerSignature Off
</VirtualHost>



Phusion Passenger users guide


�����ɏ����Ă���ʂ�B
�wPassengerHighPerformance�x��off�ɂ��Ȃ��ƁAmod_rewrite�͓��삵�Ȃ��B
�p�����[�^���Ɋ��҂��āA�‚��‚��won�x�ɂ��Ă��܂������ׂ��



posted by ���傤�� at 16:14| Comment(28) | TrackBack(1) | �‹� | ̃uO̓ǎ҂ɂȂ | XV`FbN

2009�N06��17��

flash �f�[�^�����X��ʂɎc�闝�R

flash ���b�Z�[�W���Ӑ}������ʂ���J�ڌ�Ɏc���Ă��܂��Ƃ�������B
����́Aflash ���Q��ڂ̃��N�G�X�g�܂ŕێ������Z�b�V�����X�R�[�v�̃f�[�^������B
���b�Z�[�W�ȊO�ɂ��g�p�”\�����A�����炭���_�C���N�g��̃��b�Z�[�W�p�ɗp�ӂ���Ă�����̂��낤�B

���_�C���N�g���Ȃ��Ƃ��́A���̎��̃��N�G�X�g�̃��X�|���X�Ɏc���Ă��܂��B
���̂Ƃ��́A���̂悤�� flash �֑������B

flash.now[:notice] = "���_�C���N�g���܂���ł����B"

���o���Ƃ��i�r���[�j�́A�ʏ�Ɠ����B

<%= flash[:notice] %>

�ł��B

posted by ���񂢂����� at 10:45| Comment(17) | TrackBack(0) | ��{ | ̃uO̓ǎ҂ɂȂ | XV`FbN

2009�N05��28��

has_and_belongs_to_many��foreign_key��symbol���g���Ȃ� (2.0.2)


def User < ActiveRecord::Base
has_and_belongs_to_many :clubs, :join_table=>:club_members, :foreign_key=>:member_id
end
user = User.find(1)
user.club_ids = [11,12]
# INSERT INTO club_members(`club_id`) VALUES(11)
# INSERT INTO club_members(`club_id`) VALUES(12)
# member_id������Ȃ��I



def User < ActiveRecord::Base
has_and_belongs_to_many :clubs, :join_table=>:club_members, :foreign_key=>"member_id"
end
user = User.find(1)
user.club_ids = [11,12]
# INSERT INTO club_members(`club_id`,`user_id`) VALUES(11,1)
# INSERT INTO club_members(`club_id`,`user_id`) VALUES(12,1)
posted by ������ at 14:27| Comment(29) | TrackBack(0) | ��{ | ̃uO̓ǎ҂ɂȂ | XV`FbN

model�̏�������select�w��

model�̏�������after_initialize���g���܂����A���̂悤�ȃP�[�X�ŃG���[�ƂȂ�܂��B

class User < ActiveRecord::Base
def after_initialize
self.gender ||= "male"
end
end
User.find(1) # OK
Usre.find(1, :select=>"id,age") # ERROR
# ActiveRecord::MissingAttributeError: missing attribute: gender

����́Aselect�w�肵���ꍇ�ɁAgender��attribute���C���X�^���X�ɖ������ߋN����G���[�ł��B�Ȃ̂ŁA�璷�ł����A���̂悤�ɏ����Ă����������������܂���B���ɁA���s�V�X�e���ŏ��������鍀�ڂ�lj�����Ƃ��ȂǂɁB

class User < ActiveRecord::Base
def after_initialize
self.gender ||= "male" if self.has_attribude?(:gender)
end
end
User.find(1) # OK
Usre.find(1, :select=>"id,age") # OK
# ActiveRecord::MissingAttributeError: missing attribute: gender


posted by ������ at 10:45| Comment(15) | TrackBack(0) | ��{ | ̃uO̓ǎ҂ɂȂ | XV`FbN

2009�N05��27��

ActiveRecord��find��include

ActiveRecord��select��include�𓯎��Ɏw�肷��ƁAinclude��select���㏑�����āAselect�̎w�肪�����ɂȂ��Ă��܂��܂��B
posted by ������ at 18:41| Comment(13) | TrackBack(0) | ��{ | ̃uO̓ǎ҂ɂȂ | XV`FbN

2009�N02��27��

float��B

���܂ŋC�Â��Ȃ��������ǁAruby��float����C��float�Ȃ񂾂ˁB
�Ȃ̂ŁA

1.2 - 1.0 == 0.2
=> false

�݂����Ȃ��Ƃ��N����B
����ȂƂ��́Adecimal�Ɍ���B
Rails�ł����to_d���ă��\�b�h������܂��B

1.2.to_d - 1.0.to_d == 0.2.to_d
=> true

posted by ������ at 23:26| Comment(16) | TrackBack(0) | ��{ | ̃uO̓ǎ҂ɂȂ | XV`FbN

�L��


���̍L����60���ȏ�X�V���Ȃ��u���O�ɕ\��������Ă���܂��B

�ȉ��̂����ꂩ�̕��@�Ŕ�\���ɂ��邱�Ƃ��”\�ł��B

�E�L���̓��e�A�ҏW�������Ȃ�
�E�}�C�u���O�́y�ݒ�z ���@�y�L���ݒ�z ���A�u60���ԍX�V�������ꍇ�v �� �u�L����\�����Ȃ��v�Ƀ`�F�b�N�����ĕۑ�����B