Rails 新发明,婊子不能有朋友

syutran · 2013年01月29日 · 最后由 zgm 回复于 2013年01月30日 · 4104 次阅读

俺的婊子有一列命名:friend,用于存放好友的 user_id。无论如何都不能给它赋值,郁闷了半天,只好改了列名:friend_id,才算过了关。

还是不知道是什么原因,婊子的列名还是有限制的,莫非?

这算是瞎扯淡么?

冤枉啊,@吕胖子,在下说的句句实言,还望大人明察。 你给它武值,它就报: ActiveRecord::AssociationTypeMismatch: expected, got Fixnum 俺就在武值时加上 to_s,它就 ActiveRecord::AssociationTypeMismatch: expected, got String 改为 friend_id 就安静了。

1.9.3-p286 :002 > c = Circle.new => # 1.9.3-p286 :003 > c.friend = "1" ActiveRecord::AssociationTypeMismatch: User(#89151910) expected, got String(#78194320) from /home/syutran/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.9/lib/active_record/associations/association.rb:204:in raise_on_type_mismatch' from /home/syutran/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.9/lib/active_record/associations/belongs_to_association.rb:6:inreplace' from /home/syutran/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.9/lib/active_record/associations/singular_association.rb:17:in writer' from /home/syutran/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.9/lib/active_record/associations/builder/association.rb:51:inblock in define_writers' from (irb):3 from /home/syutran/.rvm/gems/ruby-1.9.3-p286/gems/railties-3.2.9/lib/rails/commands/console.rb:47:in start' from /home/syutran/.rvm/gems/ruby-1.9.3-p286/gems/railties-3.2.9/lib/rails/commands/console.rb:8:instart' from /home/syutran/.rvm/gems/ruby-1.9.3-p286/gems/railties-3.2.9/lib/rails/commands.rb:41:in <top (required)>' from script/rails:6:inrequire' from script/rails:6:in <main>' 1.9.3-p286 :004 > c.friend = "1".to_i ActiveRecord::AssociationTypeMismatch: User(#89151910) expected, got Fixnum(#78191590) from /home/syutran/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.9/lib/active_record/associations/association.rb:204:inraise_on_type_mismatch' from /home/syutran/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.9/lib/active_record/associations/belongs_to_association.rb:6:in replace' from /home/syutran/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.9/lib/active_record/associations/singular_association.rb:17:inwriter' from /home/syutran/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.9/lib/active_record/associations/builder/association.rb:51:in block in define_writers' from (irb):4 from /home/syutran/.rvm/gems/ruby-1.9.3-p286/gems/railties-3.2.9/lib/rails/commands/console.rb:47:instart' from /home/syutran/.rvm/gems/ruby-1.9.3-p286/gems/railties-3.2.9/lib/rails/commands/console.rb:8:in start' from /home/syutran/.rvm/gems/ruby-1.9.3-p286/gems/railties-3.2.9/lib/rails/commands.rb:41:in' from script/rails:6:in require' from script/rails:6:in

' 1.9.3-p286 :005 > c.friend = 1 ActiveRecord::AssociationTypeMismatch: User(#89151910) expected, got Fixnum(#78191590) from /home/syutran/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.9/lib/active_record/associations/association.rb:204:in raise_on_type_mismatch' from /home/syutran/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.9/lib/active_record/associations/belongs_to_association.rb:6:inreplace' from /home/syutran/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.9/lib/active_record/associations/singular_association.rb:17:in writer' from /home/syutran/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.9/lib/active_record/associations/builder/association.rb:51:inblock in define_writers' from (irb):5 from /home/syutran/.rvm/gems/ruby-1.9.3-p286/gems/railties-3.2.9/lib/rails/commands/console.rb:47:in start' from /home/syutran/.rvm/gems/ruby-1.9.3-p286/gems/railties-3.2.9/lib/rails/commands/console.rb:8:instart' from /home/syutran/.rvm/gems/ruby-1.9.3-p286/gems/railties-3.2.9/lib/rails/commands.rb:41:in <top (required)>' from script/rails:6:inrequire' from script/rails:6:in <main>' 1.9.3-p286 :006 > c.friend = 1111 ActiveRecord::AssociationTypeMismatch: User(#89151910) expected, got Fixnum(#78191590) from /home/syutran/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.9/lib/active_record/associations/association.rb:204:inraise_on_type_mismatch' from /home/syutran/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.9/lib/active_record/associations/belongs_to_association.rb:6:in replace' from /home/syutran/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.9/lib/active_record/associations/singular_association.rb:17:inwriter' from /home/syutran/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.9/lib/active_record/associations/builder/association.rb:51:in block in define_writers' from (irb):6 from /home/syutran/.rvm/gems/ruby-1.9.3-p286/gems/railties-3.2.9/lib/rails/commands/console.rb:47:instart' from /home/syutran/.rvm/gems/ruby-1.9.3-p286/gems/railties-3.2.9/lib/rails/commands/console.rb:8:in start' from /home/syutran/.rvm/gems/ruby-1.9.3-p286/gems/railties-3.2.9/lib/rails/commands.rb:41:in' from script/rails:6:in require' from script/rails:6:in' 1.9.3-p286 :007 > c => # 1.9.3-p286 :008 > c.frind = a NameError: undefined local variable or method a' for main:Object from (irb):8 from /home/syutran/.rvm/gems/ruby-1.9.3-p286/gems/railties-3.2.9/lib/rails/commands/console.rb:47:instart' from /home/syutran/.rvm/gems/ruby-1.9.3-p286/gems/railties-3.2.9/lib/rails/commands/console.rb:8:in start' from /home/syutran/.rvm/gems/ruby-1.9.3-p286/gems/railties-3.2.9/lib/rails/commands.rb:41:in' from script/rails:6:in require' from script/rails:6:in' 1.9.3-p286 :009 > c.frind = "a" NoMethodError: undefined method frind=' for #<Circle:0xa890030> from /home/syutran/.rvm/gems/ruby-1.9.3-p286/gems/activemodel-3.2.9/lib/active_model/attribute_methods.rb:407:inmethod_missing' from /home/syutran/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.9/lib/active_record/attribute_methods.rb:149:in method_missing' from (irb):9 from /home/syutran/.rvm/gems/ruby-1.9.3-p286/gems/railties-3.2.9/lib/rails/commands/console.rb:47:instart' from /home/syutran/.rvm/gems/ruby-1.9.3-p286/gems/railties-3.2.9/lib/rails/commands/console.rb:8:in start' from /home/syutran/.rvm/gems/ruby-1.9.3-p286/gems/railties-3.2.9/lib/rails/commands.rb:41:in' from script/rails:6:in require' from script/rails:6:in' 1.9.3-p286 :010 > c.friend = "a" ActiveRecord::AssociationTypeMismatch: User(#89151910) expected, got String(#78194320) from /home/syutran/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.9/lib/active_record/associations/association.rb:204:in raise_on_type_mismatch' from /home/syutran/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.9/lib/active_record/associations/belongs_to_association.rb:6:inreplace' from /home/syutran/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.9/lib/active_record/associations/singular_association.rb:17:in writer' from /home/syutran/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.9/lib/active_record/associations/builder/association.rb:51:inblock in define_writers' from (irb):10 from /home/syutran/.rvm/gems/ruby-1.9.3-p286/gems/railties-3.2.9/lib/rails/commands/console.rb:47:in start' from /home/syutran/.rvm/gems/ruby-1.9.3-p286/gems/railties-3.2.9/lib/rails/commands/console.rb:8:instart' from /home/syutran/.rvm/gems/ruby-1.9.3-p286/gems/railties-3.2.9/lib/rails/commands.rb:41:in <top (required)>' from script/rails:6:inrequire' from script/rails:6:in <main>' 1.9.3-p286 :011 > c.friend = 1 ActiveRecord::AssociationTypeMismatch: User(#89151910) expected, got Fixnum(#78191590) from /home/syutran/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.9/lib/active_record/associations/association.rb:204:inraise_on_type_mismatch' from /home/syutran/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.9/lib/active_record/associations/belongs_to_association.rb:6:in replace' from /home/syutran/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.9/lib/active_record/associations/singular_association.rb:17:inwriter' from /home/syutran/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.9/lib/active_record/associations/builder/association.rb:51:in block in define_writers' from (irb):11 from /home/syutran/.rvm/gems/ruby-1.9.3-p286/gems/railties-3.2.9/lib/rails/commands/console.rb:47:instart' from /home/syutran/.rvm/gems/ruby-1.9.3-p286/gems/railties-3.2.9/lib/rails/commands/console.rb:8:in start' from /home/syutran/.rvm/gems/ruby-1.9.3-p286/gems/railties-3.2.9/lib/rails/commands.rb:41:in' from script/rails:6:in require' from script/rails:6:in'

I am sorry. It because in User with has_many :friend, :class_name => "Circle", :foriegn_key => "friend"

这哥们为什么喜欢打婊子。。。 以为你以前的帖子是打错了 现在才发现,你一直是这样打的。

看不懂在说什么。

@chucai 有歧视女性的感觉。

各位兄台,结帖。。。。。。

俺在 好友的档里有一列名是 friend,用于记录互加好友与分组。在关联表时,因为对同一表要关联两次,所以就在 User 的表里加了两行

has_many :master, :class_name => "Circle", :foriegn_key => "user_id" has_many :friend, :class_name => "Circle", :foriegn_key => "friend"

所以造成 Circle.friend 无法赋值。

原以为自己发明了新东西,谁知—— SB 总是一次又一次滴证明自己是一个 SB。

表就是表,要么叫 Table。干吗要用**?一股老流氓气息扑面而来。要问问题就正经点,不要拿出一副自认为豪爽的“雅痞”作风。

@ugoa , I am sorry. 如果您能在俺的帖子里看出一股老流氓气息来,十分感谢。另外,俺从来没自为认好爽过,感谢兄台夸赞。 是不是我忘记了查看 ruby-china 的规则?Let's me see.

==本帖已结,10 楼 回复无效==

是有点。

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