论坛首页 入门技术论坛

sql基础一定要学好

浏览 1904 次
该帖已经被评为新手帖
作者 正文
   发表时间:2010-04-09   最后修改:2010-04-10
遇到这样的一个问题.看代码
在work_sheet的model中
  named_scope :working ,:conditions => ["qted = ? and start > ? ",4,DateTime.now]
  named_scope :starting,:conditions => "production_plans.state = 0", :include => :production_plan

#不报错
WorkSheet.working
#不报错
WorkSheet.starting
#报错
WorkSheet.starting.working

不明白为什么会这样?报错如下:
Mysql::Error: Column 'start' in where clause is ambiguous: SELECT `work_sheets`.* FROM `work_sheets` INNER JOIN `production_plans` ON `production_plans`.id = `work_sheets`.production_plan_id WHERE ((production_plans.state = 0) AND (qted = 4 and start > '2010-04-09 14:14:50' ))

sql跑了下也是报错,没有仔细研究,就拿去请教别人去了.ambiguous原来是模棱两可的意思.在worksheet和production_plan里都有start字段,然后关联后,就无法确认是哪个的start字段了.自己再改成这样,然后试了下.可以了.
named_scope :working ,:conditions => ["qted = ? and work_sheets.start > ? ",4,DateTime.now]
   发表时间:2010-04-09  
Robbin真是狠准快啊。这贴这么快就被打到新手贴了。因为正上班,草草就结贴了。本来还想等回去在补充下的,不过看来没机会了。等家里能上网了,在好好发一贴。je手机能登录了,感觉挺不错的。
0 请登录后投票
   发表时间:2010-04-10  
让我们看看其他人是怎么做的.
>> WorkSheet.production_plan_state_equals(0)
  ProductionPlan Columns (1.4ms)   SHOW FIELDS FROM `production_plans`
  WorkSheet Load (0.5ms)   SELECT `work_sheets`.* FROM `work_sheets` INNER JOIN `production_plans` ON `production_plans`.id = `work_sheets`.production_plan_id WHERE (production_plans.state = 0) 
+----+-------+-------+-------+-------+------+-------+-------+-------+-------+-------+-------+-------+------+-------+-------+------+------+-------+-------+
| id | nu... | my... | cr... | up... | name | de... | de... | eq... | start | end   | re... | di... | note | qu... | pr... | rate | qted | ch... | de... |
+----+-------+-------+-------+-------+------+-------+-------+-------+-------+-------+-------+-------+------+-------+-------+------+------+-------+-------+
| 82 | GZ... | 105   | 20... | 20... | 下料   |       |       |       | 20... | 20... |       | 0     |      | 3     | 32    | 0.2  | 1    | 1     | 0     |
| 83 | GZ... | 105   | 20... | 20... | 下料   |       |       |       | 20... | 20... |       | 0     |      | 3     | 32    | 0.2  | 1    | 1     | 0     |
| 84 | GZ... | 105   | 20... | 20... | 下料   |       |       |       | 20... | 20... |       | 0     |      | 3     | 32    | 0.2  | 1    | 1     | 0     |
| 85 | GZ... | 105   | 20... | 20... | 下料   |       |       |       | 20... | 20... |       | 0     |      | 2     | 32    | 0.2  | 0    | 1     | 0     |
+----+-------+-------+-------+-------+------+-------+-------+-------+-------+-------+-------+-------+------+-------+-------+------+------+-------+-------+
4 rows in set
>> WorkSheet.qted_equals(2)
  WorkSheet Load (0.4ms)   SELECT * FROM `work_sheets` WHERE (work_sheets.qted = 2) 
=> []

至少在searchlogic里面,如果这样写的话,生成的named_scope,每个表无论是否和其他表发生关联,都会加上这样的一个表名.看来这样做是最保险的了.
0 请登录后投票
论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics