2012-01-30 71 views
2

我在我的RoR3.2.1應用程序中通過關係直接轉發了has_many。唯一的變化是我正在使用一個不同的外鍵/主鍵,因爲我使用的是舊數據庫。Rails3國外主鍵錯誤圓形

出於某種原因,當我創建新記錄時,這些值將保存在錯誤的字段中。組名保存到用戶名中,反之亦然。

我的radusers通過radusergroups有很多radgroups。我的連接表有列用戶名和組名。

Raduser.rb:

has_many :radusergroup, :dependent => :destroy, :foreign_key => :groupname, :primary_key => :username 
has_many :radgroup, :through => :radusergroup 

Radgroup.rb:

has_many :radusergroup, :dependent => :destroy 
has_many :raduser, :through => :radusergroup 

Radusergroup.rb

belongs_to :raduser, :primary_key => :username 
belongs_to :radgroup, :primary_key => :groupname, :foreign_key => :username 

我在表單中有這樣的:

- Radgroup.all.each do |radgroup| 
    = check_box_tag "raduser[radgroup_ids][]", radgroup.id, @raduser.radgroup.include?(radgroup), :id => "raduser_radgroup_id_#{radgroup.id}" 
    = radgroup.groupname 

當我打救,我的記錄保存周圍走錯了路:

INSERT INTO `radusergroup` (`groupname`, `id`, `priority`, `username`) VALUES ('username1233', NULL, 1, 'groupname123') 

而且表:

+------------------+--------------+----------+----+ 
| username   | groupname | priority | id | 
+------------------+--------------+----------+----+ 
| groupname123  | username1233 |  1 | 1 | 
+------------------+--------------+----------+----+ 

任何人在那裏能夠幫助呢?

----編輯添加中微量按回答以下----

actionpack (3.2.1) lib/action_controller/metal/hide_actions.rb:36:in `visible_action?' 
actionpack (3.2.1) lib/action_controller/metal/hide_actions.rb:18:in `method_for_action' 
actionpack (3.2.1) lib/action_controller/metal/implicit_render.rb:14:in `method_for_action' 
actionpack (3.2.1) lib/action_controller/metal/compatibility.rb:61:in `method_for_action' 
actionpack (3.2.1) lib/abstract_controller/base.rb:115:in `process' 
actionpack (3.2.1) lib/abstract_controller/rendering.rb:45:in `process' 
actionpack (3.2.1) lib/action_controller/metal.rb:203:in `dispatch' 
actionpack (3.2.1) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch' 
actionpack (3.2.1) lib/action_controller/metal.rb:246:in `block in action' 
actionpack (3.2.1) lib/action_dispatch/routing/route_set.rb:66:in `call' 
actionpack (3.2.1) lib/action_dispatch/routing/route_set.rb:66:in `dispatch' 
actionpack (3.2.1) lib/action_dispatch/routing/route_set.rb:30:in `call' 
journey (1.0.1) lib/journey/router.rb:68:in `block in call' 
journey (1.0.1) lib/journey/router.rb:56:in `each' 
journey (1.0.1) lib/journey/router.rb:56:in `call' 
actionpack (3.2.1) lib/action_dispatch/routing/route_set.rb:589:in `call' 
warden (1.1.0) lib/warden/manager.rb:35:in `block in call' 
warden (1.1.0) lib/warden/manager.rb:34:in `catch' 
warden (1.1.0) lib/warden/manager.rb:34:in `call' 
actionpack (3.2.1) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call' 
rack (1.4.1) lib/rack/etag.rb:23:in `call' 
rack (1.4.1) lib/rack/conditionalget.rb:25:in `call' 
actionpack (3.2.1) lib/action_dispatch/middleware/head.rb:14:in `call' 
actionpack (3.2.1) lib/action_dispatch/middleware/params_parser.rb:21:in `call' 
actionpack (3.2.1) lib/action_dispatch/middleware/flash.rb:242:in `call' 
rack (1.4.1) lib/rack/session/abstract/id.rb:205:in `context' 
rack (1.4.1) lib/rack/session/abstract/id.rb:200:in `call' 
actionpack (3.2.1) lib/action_dispatch/middleware/cookies.rb:338:in `call' 
activerecord (3.2.1) lib/active_record/query_cache.rb:64:in `call' 
activerecord (3.2.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:443:in `call' 
actionpack (3.2.1) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call' 
activesupport (3.2.1) lib/active_support/callbacks.rb:405:in `_run__2911347278127141110__call__2925805972906996448__callbacks' 
activesupport (3.2.1) lib/active_support/callbacks.rb:405:in `__run_callback' 
activesupport (3.2.1) lib/active_support/callbacks.rb:385:in `_run_call_callbacks' 
activesupport (3.2.1) lib/active_support/callbacks.rb:81:in `run_callbacks' 
actionpack (3.2.1) lib/action_dispatch/middleware/callbacks.rb:27:in `call' 
actionpack (3.2.1) lib/action_dispatch/middleware/reloader.rb:65:in `call' 
actionpack (3.2.1) lib/action_dispatch/middleware/remote_ip.rb:31:in `call' 
actionpack (3.2.1) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call' 
actionpack (3.2.1) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call' 
railties (3.2.1) lib/rails/rack/logger.rb:26:in `call_app' 
railties (3.2.1) lib/rails/rack/logger.rb:16:in `call' 
actionpack (3.2.1) lib/action_dispatch/middleware/request_id.rb:22:in `call' 
rack (1.4.1) lib/rack/methodoverride.rb:21:in `call' 
rack (1.4.1) lib/rack/runtime.rb:17:in `call' 
activesupport (3.2.1) lib/active_support/cache/strategy/local_cache.rb:72:in `call' 
rack (1.4.1) lib/rack/lock.rb:15:in `call' 
actionpack (3.2.1) lib/action_dispatch/middleware/static.rb:53:in `call' 
railties (3.2.1) lib/rails/engine.rb:479:in `call' 
railties (3.2.1) lib/rails/application.rb:220:in `call' 
rack (1.4.1) lib/rack/content_length.rb:14:in `call' 
railties (3.2.1) lib/rails/rack/log_tailer.rb:14:in `call' 
rack (1.4.1) lib/rack/handler/webrick.rb:59:in `service' 
/usr/local/lib/ruby/1.9.1/webrick/httpserver.rb:111:in `service' 
/usr/local/lib/ruby/1.9.1/webrick/httpserver.rb:70:in `run' 
/usr/local/lib/ruby/1.9.1/webrick/server.rb:183:in `block in start_thread' 

--- SESSION DUMP ---

_csrf_token: "YepneklJ9OWdmKHVVclxGzrg5ECbk11a9B3tJJtoECU=" 
session_id: "8cec81c93085d17dc744f1a44a7054b6" 
warden.user.user.key: ["User", [1], "$2a$10$J.oF0HRrE0PokqkQ2S4gwO"] 

回答

2

我相信沒有:爲primary_key選項關聯。這必須在課堂上進行定義。你可以嘗試改變你的類此:

Raduser.rb:

primary_key :username 

has_many :radusergroup, :dependent => :destroy, :foreign_key => :username 
has_many :radgroup, :through => :radusergroup 

Radgroup.rb:

primary_key :groupname 

has_many :radusergroup, :dependent => :destroy, :foreign_key => :radgroup 
has_many :raduser, :through => :radusergroup 

Radusergroup.rb

belongs_to :raduser, :foreign_key => :username 
belongs_to :radgroup, :foreign_key => :groupname 
+0

感謝。這是有道理的 - 嘗試過這個雖然我得到一個錯誤:未定義的方法'鍵?'爲零:NilClass。 – simonmorley 2012-01-31 07:59:14

+0

你可以打印堆棧跟蹤嗎? – fkreusch 2012-01-31 11:05:17

+0

剛剛更新了這個問題。 – simonmorley 2012-01-31 23:05:44