1
class Department < ActiveRecord::Base

attr_accessible :Department_Id, :DepartmentName,:LocationID
set_primary_key :Department_Id 
has_many :Employee

end

导轨控制台:

dept=Department.find(50)
dept.Employee 
SELECT `employees`.* FROM `employees` WHERE `employees`.`Department_Id` IS NULL

为什么它在where子句中附加NULL,我期待看到50。我做错了什么

4

2 回答 2

0

在 ruby​​ 中,以大写字母开头的变量是常量变量。您的代码应更改为:

class Department < ActiveRecord::Base

attr_accessible :department_id, :department_name,:location_id
has_many :employees

end

更新:我只是举了一些例子。您需要相应地更改所有变量、数据库列名等。请确保一切都符合规则:常量变量以大写字母和局部变量开头,实例变量都是小写,其中单词用破折号分隔。

如果您的代码符合规则,则不必调用set_primary_key和设置foreign_keybelongs_to 关联。

于 2012-11-28T00:07:59.637 回答
0

您必须遵循适当的约定,大写字母的变量表示常量变量,请尝试如下

class Department < ActiveRecord::Base

  attr_accessible :Department_Id, :DepartmentName,:LocationID
  set_primary_key :Department_Id 
  has_many :employee

end

如果你这样做,甚至会更好

class Department < ActiveRecord::Base

  attr_accessible :department_id, :department_name,:location_id
  set_primary_key :department_id 
  has_many :employee

end
于 2012-11-28T01:30:47.333 回答