俺的婊子有一列命名: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: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: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:in
block 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:in
start'
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:in
require'
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: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: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: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: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:in
start'
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
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: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: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:in
block 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:in
start'
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:in
require'
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: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: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: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: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:in
start'
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:in
start'
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:in
method_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:in
start'
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: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: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:in
block 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:in
start'
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:in
require'
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: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: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: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: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:in
start'
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"
各位兄台,结帖。。。。。。
俺在 好友的档里有一列名是 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。
@ugoa , I am sorry.
如果您能在俺的帖子里看出一股老流氓气息
来,十分感谢。另外,俺从来没自为认好爽
过,感谢兄台夸赞。
是不是我忘记了查看 ruby-china 的规则?Let's me see.