2012-04-07 79 views
0

我真的被困在這個問題上,我不能找到一種方法,從它問世:嵌套的資源有很多:通過關係問題

我有一個用戶模式(內置了色器件)。

我擁有的項目模型,是用戶的嵌套資源。

在用戶模型(user.rb):

# Setup the relations model 
has_many :items, :dependent => :destroy 

在項目模型(item.rb的):

resources :users do 
    resources :items 
end 

# Setup the relations model 
belong_to :user 
在routes.rb中

以這種方式,用戶能夠創建一個項目,並擁有它:

ex of path:user/1/items/1

現在我希望用戶能夠與另一個用戶共享一個項目(由他創建),但是保留項目的所有權(只是用戶創建該項目的用戶可以銷燬該項目,並且只有創建該項目的用戶可以更新該項目的某個字段,而其他字段也需要由用戶接收該項目才能更新(可編輯))。 換句話說,我希望項目的權限對於創建它的用戶和剛接收它的用戶是不同的。

爲了讓用戶之間分享內容的動作,我建立以下as_many:通過關係:

我創造了另一個模型,稱爲共享(代表項目模型和用戶之間的接合表模型):

然後我修改以下列方式的user.rb(用戶模型):

# Setup the Item relations model 
has_many :items, :dependent => :destroy # this to set up the item ownership 

#This to set up the has_many :through realtionship 
#(note I am calling a new model shared_items - that really doesn't exist - is an item model) 
has_many :sharings 
has_many :shared_items, :foreign_key => "shared_user_id", :through => :sharings 

accepts_nested_attributes_for :items #This will work for the nested items resource 
accepts_nested_attributes_for :sharings #Do I really need this line?? 

# Setup accessible (or protected) attributes for your model 
attr_accessible :email, :password, :password_confirmation, 
       :items_attributes, #This will work for the nested items resource 
       :sharings_attributes #Do I really need this line?? 

然後我修改以下列方式的item.rb的(項目模型):

# Setup the relations model 
belong_to :user 

#this two lines are suppose to connect the two models (item and receiver) using a joint table 
has_many :sharings 
has_many :shared_users, :foreign_key => "shared_item_id", :through => :sharings 

accepts_nested_attributes_for :sharings #Do I really need this line?? 

attr_accessible :user_id, :item_type, :reference_date, :title, 
       :sharings_attributes #Do I really need this line?? 

我寫的sharing.rb(共享模式 - 聯合表),以下列方式:

belongs_to :shared_user, :class_name => "User" 
belongs_to :shared_item, :class_name => "Item" 

attr_accessible :shared_moment_id, :shared_user_id 

後,我不得不考慮的是,接收器也是一個用戶(是自我參考關係),並且在我的應用程序中,仍然存在友情模型(允許用戶與其他用戶進行交流,這就像一種魅力) - 但我相信Joel的建議可以幫助(或者只是改變參考我的朋友表)。

在這裏,我可以爲Item(ItemsController)創建一個寧靜的控制器,並添加一個新的,創建,顯示,銷燬等操作,允許用戶(實際上是current_user)創建一個項目,銷燬或更新它沒有任何問題。 (正如你可以在項目模型中看到的那樣有外鍵user_id)。

我需要做的,以創造一個寧靜的控制器來管理共享模式(創建新的共享項目,並把它們(分享)與其他用戶分配),而不是什麼???

後如何我記得數據庫值?我的意思是,例如:

用戶ID爲3創建了五個新項目(同項目編號1,2,3,4,5),現在他也決定要分享一些與其他用戶(第2項與本項目用戶6,具有用戶7,8,9項4,5)???

我拉出了共享嵌套的資源,現在我只是有:

產品的用戶一個嵌套的資源(用戶/ 2 /項/ 5) 共享將是什麼?

請人幫我....

非常感謝 Dinuz

UPDATE 我能夠讓正常工作的應用程序,但由於只有單件:

的用戶創建一個項目。在他決定與另一個用戶共享該項目(並使用分享控制器 - 聯合表格控制器後,我能夠在聯合表格內創建記錄(傳遞用戶標識和特定項目標識)

現在我想代之以一次性執行所有操作:

current_user(登錄的用戶)創建一個項目,並且他需要以相同的形式有機會與其他人共享用戶(陣列)或只是爲自己保留。

我相信,這一進一步的步驟,需要融化在一起的項目控制器和共享控制器,並在新項目視圖中做同樣的事情。

那麼如何我需要繼續嗎?如果有人可以爲has_many提出一個很好的教程:通過它可以覆蓋我的案例(我真的需要了解在這種情況下控制器和視圖的外觀),我認爲模型關聯設置得很好,但是我無法真正弄清楚如何與控制器和視圖進行。

回答

1

您可以使用自定義驗證這樣的:

class User < ActiveRecord::Base 
    validate :sharing_with_friends_only 

    def sharing_with_friends_only 
    (receivers - friends).empty? 
    end 
end 

至於你提到的,最簡單的方式就是取消窩的資源。

這也將使你的API更清潔。

+0

嗨喬爾,你的想法是一個很好的,但我需要弄清楚我需要如何設置模型中的關聯 – Dinuz 2012-04-07 23:54:23

+0

我要編輯我的文章,以更新問題。 – Dinuz 2012-04-07 23:56:11

+0

好吧,我解決了這個問題....再次感謝joel! – Dinuz 2012-04-14 22:07:18