查询无法找到列

时间:2012-01-07 22:15:25

标签: ruby-on-rails-3

我一直在尝试让这个查询在过去几个小时内完成,但我不能。所以我希望有人能提供帮助。

这是错误: Mysql ::错误:'where子句'中的未知列'network_id':SELECT networks。* FROM networks WHERE(network_id = 1,2)

以下是我的模特:

class Network < ActiveRecord::Base
  belongs_to :customer
  has_many :user_network_relations

class Customer < ActiveRecord::Base
  has_one :network, :dependent=>:destroy
  accepts_nested_attributes_for :network

class UserNetworkRelation < ActiveRecord::Base
  belongs_to :network
  accepts_nested_attributes_for :network

控制器

@user = User.find(params[:id])
@user_approved = UserNetworkRelation.find(:all,:conditions => ['user_id = ? and status =? ', @user, "approved"])
@networks = Network.find(:all,:conditions => ['network_id = ?',@user_approved])
@user_networks = Customer.find(@networks)    

感谢任何帮助。提前谢谢!

1 个答案:

答案 0 :(得分:1)

错误在这一行:

@networks = Network.find(:all,:conditions => ['network_id = ?',@user_approved])

您明确告诉ActiveRecord您要限制显然不存在的列network_id。可能你的意思是id列。正确的行可以如下所示:

@networks = Network.where(:id => @user_approved).all

或等效

@networks = Network.where('id = ?', @user_approved).all

请注意,在我的示例中,我使用了Rails 3的新的ActiveModel / AREL语法。您使用了已弃用(但仍然支持)的Rails语法2.您应该切换到新语法,因为它允许您链接查询更强大,更可读。请参阅documentation,了解如何使用新的AREL语法。