Rails中文Podcasts【三】

一、session中来自model的危险

在session中存model的时候需要谨慎些,有时候它会变得很不听话而且特尼玛容易和数据库不同步,所以最好的方式不是存model在session中,而是存model的ID,并从数据库中根据ID来抓model

class UsersController < ApplicationController
  def prepare
    session[:user] = User.find(:user)
    redirect_to action: 'show'
  end
  
  def show
    @user = session[:user]
  end
  
  def update
    @user = session[:user]
    @user.name = "Foo"
    redirect_to action: 'show'
  end
end
这样写当你在show页面update的时候只能改变存在session中user的name,数据库中的依然没有变,那么就会出现各种问题,当然你就只能呵呵了

科学的写法应该是这个样子滴

class UsersController < ApplicationController
  def prepare
    session[:user_id] = User.find(:user).id
    redirect_to action: 'show'
  end
  
  def show
    @user = User.find(session[:user_id])
  end
  
  def update
    @user = User.find(session[:user_id])
    @user.name = "Foo"
    redirect_to action: 'show'
  end
end

二、在model中执行计算

你知道吗,其实在model中,ActiveRecord提供了类方法来计算一些东西,这些继承自ActiveRecord的方法甚至能让你在关联的时候使用

打开console(话说Ruby2.0开启console的速度杠杠的,童鞋快去down下来试试吧)

>>Task.find(:first).priority
=>4
然后你可以用sum来算总和
>>Task.sum(:priority)
=>15
在sum的时候传些限制级别的条件也是可以的
>>Task.sum(:priority, conditions: 'complete=0')
=>13
还有更cool的,你甚至可以找最大值、最小值还有平均值
>>Task.maximum(:priority)
=>4
>>Task.minimum(:priority)
=>1
>>Task.average(:priority)
=>2.5
如果这个Task model是belongs_to一些其他的model的,上述方法可以查询

三、有趣的find条件

你能传给condition可不仅仅是简单的string,数组、排序还有nil空值都能传。
>>Task.count(:all, conditions: ["complete=? and priority=?", false, 3])
=>2
其中的“?”是占位符,有点像c中print函数的%d
>>Task.count(:all, conditions: ["complete=? and priority=?", false, nil])
=>0
等价于下面这种写法
>>Task.count(:all, conditions: ["complete=? and priority IS ?", false, nil])
=>0
“IS”和“=”的意思一样,但是 并不意味着你可以这样
>>Task.count(:all, conditions: ["complete=? and priority IS ?", false, [1,3])#This not right
而是这样
>>Task.count(:all, conditions: ["complete=? and priority IN (?)", false, [1,3])
=>1

犹豫ruby的特性,上面的等价于下面这个
>>Task.count(:all, conditions: ["complete=? and priority IN (?)", false, 1...3)
=>1


除了形如
["complete=? and priority IN (?)", false, 1...3)
的查询语句,你可以采用ruby强横的hash句式来查询
>>Task.count(:all, conditions:  { complete: false, priority: 1})

四、虚拟属性

虚拟属性是一个很有意思的东西,它显得很灵活,比如像这样一个表单:

这是符合我们的数据库的,因为数据库中的User所具有的column就是first_name和last_name,但在现实中我们何曾把名和姓分开呢?我们当然会希望这个样子:

对应的erb是
<h1>Register</h1>
<% form_for(@user), url: users_path do |f| %>
<p>
  Full name</br>
  <%= f.text_field :full_name%>
</P>
<p>
  Password</br>
  <%= f.password_field :password%>
</P>
<p>
  <%= submit_tag 'Register'%>
</P>
<% end %>
问题来了,我们的User Model以及数据表中可是没有full_name的,你这是要逆天吗?当然不是,我们自己来定义一个就好了:
class User < ActiveRecord::Base
  attr_accessible :first_name, :last_name, :password
  
  def full_name
    [first_name, last_name].join(' ')
  end
  
  def full_name=(name)
    split = name.split('  ',2)
    self.first_name = split.first
    self.last_name = split.last
  end
end
通过join方法我们连full_name也可以直接取了,而full_name=(name)方法则把上面那个erb中的full_name给分而化之了,这样就不会在往数据库存的时候出现意外了


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值