Ruby on rails 检查Rails中是否存在关联的记录

Ruby on rails 检查Rails中是否存在关联的记录,ruby-on-rails,ruby-on-rails-3,postgresql,Ruby On Rails,Ruby On Rails 3,Postgresql,我的PostgreSQL数据库中有两个表,分别名为users和businesss。当用户登录并访问business/profile时,我首先要检查他们是否实际创建了配置文件。如果没有,我想将它们重定向到业务/create。因此,基本上,我需要做的是检查businesss表是否包含与当前用户关联的记录。我尝试了一些我认为有效的方法,但不断出现以下错误: Couldn't find User/Business without an id. 当前“我的路线”文件包含以下定义: get "busin

我的PostgreSQL数据库中有两个表,分别名为
users
businesss
。当用户登录并访问
business/profile
时,我首先要检查他们是否实际创建了配置文件。如果没有,我想将它们重定向到
业务/create
。因此,基本上,我需要做的是检查
businesss
表是否包含与当前用户关联的记录。我尝试了一些我认为有效的方法,但不断出现以下错误:

Couldn't find User/Business without an id. 
当前“我的路线”文件包含以下定义:

get "business/profile"
get "business/account"
get "business/create"

devise_for :users

get "home/index"
root :to => "home#index"

在用户和业务模型之间建立关系

class User < ActiveRecord::Base
    has_one :business
  end
class Business < ActiveRecord::Base
    belongs_to :user
end
class用户
in businees_controller.rb

class BusinessController < ApplicationController
 def profile
  redirect_to business_create_path unless current_user.business
  "your code"
 end
 def create
 end
end
class-BusinessController
我认为您使用的是
Business.find_by_u…()
方法,但如果找不到任何东西,它会给您404错误

您应通过以下方式进行检查:

def profile
  if current_user.businesses.count == 0
    redirect_to business_create_path
  else
    # ...
  end
end
我假设您的模型中有一个User::bellings_to(Business)关系