Rails 关于 rails 操作 MYSQL 的问题

xoxoj · 发布于 2012年04月12日 · 最后由 xoxoj 回复于 2012年04月12日 · 2375 次阅读
96

我看了很多RAILS的例子,都是用scaffold 创建的。包括到数据库rake db:migrate rake db:create之类的,而我则选择通过运行SQL代码,或者使用PHPMYADMIN来直接创建和维护MYSQL。

这样可不可以呢,我试过,是可以的。在RAILS下,我再手动创建了models下的对应rb文件,例如我在phpmyadmin里面创建了一个users表,那么我就在rails models下创建一个user.rb的文件。

大致内容如下:

class User < ActiveRecord::Base attr_accessible :email, :unn has_many :microposts end

这样是没问题的。

然后我再创建了一个 microposts表,内容如下:

class Micropost < ActiveRecord::Base attr_accessible :content, :user_uid belongs_to :user end

user表里有一个uid字段,是表示用户的ID的。

然后把user和micropost关联一下 分别是 has_many 和 belongs_to关系

这个时候,我在IRB环境下进行测试: first_user = Users.first ,是可以查询到正确的数据的。但是当我first_user.microposts这样查询的时候,就出错了,出错内容如下:

NoMethodError: undefined method microposts' for #<Users:0x00000100be24f8> from /Users/JasonJiang/.rvm/gems/ruby-1.9.3-p0/gems/activemodel-3.2.3/lib/active_model/attribute_methods.rb:407:inmethod_missing' from /Users/JasonJiang/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.3/lib/active_record/attribute_methods.rb:148:in method_missing' from (irb):112 from /Users/JasonJiang/.rvm/gems/ruby-1.9.3-p0/gems/railties-3.2.3/lib/rails/commands/console.rb:47:instart' from /Users/JasonJiang/.rvm/gems/ruby-1.9.3-p0/gems/railties-3.2.3/lib/rails/commands/console.rb:8:in start' from /Users/JasonJiang/.rvm/gems/ruby-1.9.3-p0/gems/railties-3.2.3/lib/rails/commands.rb:41:in' from /Users/JasonJiang/ror/TestSQL/script/rails:6:in require' from /Users/JasonJiang/ror/TestSQL/script/rails:6:in' from -e:1:in load' from -e:1:in

'

请教大家一下,这个问题该是怎么回事啊,谢谢!

共收到 24 条回复
96

rails g modle micropost

96

啊。已经创建了啊。

1093

first_user.micropost.content有结果不

96

没有的,显示如下:

irst_user.micropost.content NoMethodError: undefined method micropost' for #<User:0x00000101c38320> from /Users/JasonJiang/.rvm/gems/ruby-1.9.3-p0/gems/activemodel-3.2.3/lib/active_model/attribute_methods.rb:407:inmethod_missing' from /Users/JasonJiang/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.3/lib/active_record/attribute_methods.rb:148:in method_missing' from (irb):31 from /Users/JasonJiang/.rvm/gems/ruby-1.9.3-p0/gems/railties-3.2.3/lib/rails/commands/console.rb:47:instart' from /Users/JasonJiang/.rvm/gems/ruby-1.9.3-p0/gems/railties-3.2.3/lib/rails/commands/console.rb:8:in start' from /Users/JasonJiang/.rvm/gems/ruby-1.9.3-p0/gems/railties-3.2.3/lib/rails/commands.rb:41:in' from /Users/JasonJiang/ror/TestSQL/script/rails:6:in require' from /Users/JasonJiang/ror/TestSQL/script/rails:6:in' from -e:1:in load' from -e:1:in

'

1

class User first_user = Users.first

类名打错了吧

96

first_user.micropost.content NoMethodError: undefined method micropost' for #<User:0x00000101c38320> from /Users/JasonJiang/.rvm/gems/ruby-1.9.3-p0/gems/activemodel-3.2.3/lib/active_model/attribute_methods.rb:407:inmethod_missing' from /Users/JasonJiang/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.3/lib/active_record/attribute_methods.rb:148:in method_missing' from (irb):31 from /Users/JasonJiang/.rvm/gems/ruby-1.9.3-p0/gems/railties-3.2.3/lib/rails/commands/console.rb:47:instart' from /Users/JasonJiang/.rvm/gems/ruby-1.9.3-p0/gems/railties-3.2.3/lib/rails/commands/console.rb:8:in start' from /Users/JasonJiang/.rvm/gems/ruby-1.9.3-p0/gems/railties-3.2.3/lib/rails/commands.rb:41:in' from /Users/JasonJiang/ror/TestSQL/script/rails:6:in require' from /Users/JasonJiang/ror/TestSQL/script/rails:6:in' from -e:1:in load' from -e:1:in

'

96

@Rei 类名打错了? 没打错啊

first.user.email

是可以正常显示结果的

96

first_user.email(不是first.user.email, 笔误)

1

前面是 Users#microposts 6楼是 User#micropost

楼主再检查下拼写

96

检查了,那个没问题。

96

重要的错误提示我觉得应该是这句

first_user.microposts Micropost Load (0.3ms)ActiveRecord::StatementInvalid: Mysql2::Error: Unknown column SELECT microposts.* FROM microposts WHERE microposts.user_id = 1 'microposts.user_id'Mysql2::Error: Unknown column 'microposts.user_id' in 'where clause': SELECT microposts.* FROM microposts WHERE microposts.user_id = 1 in 'where clause': SELECT microposts.* FROM microposts WHERE microposts.user_id = 1

96

在我的microposts表里面,是没有user_id这个字段的,我是设置的user_uid这个字段

200

外键错了

96

外键错了? 能详细说说吗?

273

好像没遇到过这样。。

200

belongs_to :user,:foreign_key => "user_uid" #默认是user_id

96

@Ddl1st 我这样操作了,还是不行。

96

ActiveRecord::StatementInvalid: Mysql2::Error: Unknown column 'microposts.user_id' in 'where clause': SELECT microposts.* FROM microposts WHERE microposts.user_id = 1

96

还是没办法改过来

96

感觉这是个BUG?无解了?

1

has_many :microposts, :foreign_key => "user_uid"

96

class User < ActiveRecord::Base attr_accessible :email, :unn

has_many :microposts, :foreign_key => 'user_uid' end

96

已经这样做了,还是不行。。。。

96

OK,都是没有reload惹的祸呐!!!辛苦各位啦

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册