2011-07-05 168 views
0

來自下圖。多對多關係ruby on rails 3

enter image description here

可以看出,我有三個表用戶,USER_GROUP和組。我試圖將用戶鏈接到user_group,將用戶鏈接到user_group,換句話說,將用戶鏈接到user_group。目前在我的用戶模型,我有以下:

User.rb has_and_belongs_to_many:組

Group.rb has_and_belongs_to_many:用戶

什麼我基本上是試圖做的是進口數據到User_group表中。什麼是最好的解決方法。嘗試在網上搜索,並沒有運氣。

enter image description here

窗體佈局

用戶應該能夠自己分配到組。當他們點擊更新進入應保存到USER_GROUP表

+0

你的問題是什麼?我不明白你的問題。你想要導入什麼數據,爲什麼它不起作用?請提供更多相關信息。 – ayckoster

+0

我想要做的是分配一個用戶到一個組,所以基本上表user_groups將有用戶ID 1和組ID 1。我已創建一個用戶表單與指定的組的複選框,即管理員,經理,組長。用戶應能夠檢查這些框,並在完整的註冊表格中提供的信息應保存到user_group表中 – David

回答

1

如果你想使用Rails約定,您應該爲您的表usersgroupsgroups_users和變化的信息:

has_and_belongs_to_many :group 
has_and_belongs_to_many :user 

到:

has_and_belongs_to_many :groups 
has_and_belongs_to_many :users 

如果你想保持你上述的表名,你必須做到以下幾點:

class User < ActiveRecord::Base 
    set_table_name 'USER' 
    has_and_belongs_to_many :groups, :join_table => 'USER_GROUP' 
end 

class Group < ActiveRecord::Base 
    set_table_name 'GROUP' 
    has_and_belongs_to_many :users, :join_table => 'USER_GROUP' 
end 

請注意,表名是區分大小寫的。我使用大寫,但可能與您的情況不符。