开发者社区> 问答> 正文

Ruby on Rails 3无法通过OSX上的套接字'/tmp/mysql.sock'?mysql

我有一个标准的Rails3环境,RVM 1.2.9,Rails 3.0.5,Ruby 1.9.2p180,MySQL2 Gem 0.2.7,mysql-5.5.10-osx10.6-x86_64

运行rake db:migrate创建数据库时出现的错误是:

Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) config / database.yml有

development: adapter: mysql2 host: localhost username: root password: xxxx database: xxxx 确保这是我想念的简单事情。

展开
收起
保持可爱mmm 2020-05-17 18:36:23 32369 0
1 条回答
写回答
取消 提交回答
  • 该where子句永远不会返回nil,它返回一个ActiveRecord :: Relation对象,该对象可以为空!

    尝试以下方法:

    def create if @trained = Certificate.where(user_id: params[:certificate][:user_id]).first @trained.update_attributes(attend: "No") else @trained = Certificate.new(params[:certificate]) if @trained.save @trained.update_attributes(attend: "Yes") end end

    redirect_to grandstreamers_resellers_path end 但我会将代码重构为如下形式:

    def create @trained = Certificate.where(user_id: params[:certificate][:user_id]).first @trained ||= Certificate.new(params[:certificate]) if @trained.persisted? # tests if the records is persisted in the DB (has an ID) @trained.attend = "No" else @trained.attend = "Yes" end

    @trained.save redirect_to grandstreamers_resellers_path end 非常简短的版本:

    def create @trained = Certificate.where(user_id: params[:certificate][:user_id]).first || Certificate.new(params[:certificate]) @trained.attend = @trained.persisted? ? "No" : "Yes" @trained.save

    redirect_to grandstreamers_resellers_path end 为了记录(@NitinJ,请看一下),您可以在if条件中分配变量:

    if first_user = User.first

    if User.first does not returns nil, executes the first block with local variable first_user available

    first_user.id else # User.first returned nil end来源:stack overflow

    2020-05-18 13:45:27
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
One Box: 解读事务与分析一体化数据库 HybridDB for MySQL 立即下载
One Box:解读事务与分析一体化数据库HybridDB for MySQL 立即下载
如何支撑HTAP场景-HybridDB for MySQL系统架构和技术演进 立即下载

相关镜像