Rails 关于 rails 操作 MYSQL 的问题

xoxoj · 2012年04月12日 · 最后由 xoxoj 回复于 2012年04月12日 · 4020 次阅读

我看了很多 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

'

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

rails g modle micropost

啊。已经创建了啊。

first_user.micropost.content 有结果不

没有的,显示如下:

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

'

class User first_user = Users.first

类名打错了吧

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

'

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

first.user.email

是可以正常显示结果的

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

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

楼主再检查下拼写

检查了,那个没问题。

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

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

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

外键错了

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

好像没遇到过这样。。

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

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

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

还是没办法改过来

感觉这是个 BUG?无解了?

has_many :microposts, :foreign_key => "user_uid"

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

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

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

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

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