1
A
回答
0
由於Shweta建議,你需要一個多對多的關係。事實上,它看起來像你需要兩個,一個用於興趣,一個用於參與。最簡單的實現依賴於has_and_belongs_to_many:
用戶模型
has_and_belongs_to_many :interests, :class_name => Event, :join_table => "interests_users"
has_and_belongs_to_many :participations, :class_name => Event, :join_table => "participations_users"
事件模型
has_and_belongs_to_many :followers, :class_name => User, :join_table => "interests_users"
has_and_belongs_to_many :participants, :class_name => User, :join_table => "participations_users"
在你的連接表遷移:
def change
create_table :interests_users, :id => false do |t|
t.integer :event_id
t.integer :user_id
end
create_table :participations_users, :id => false do |t|
t.integer :event_id
t.integer :user_id
end
end
您應該能夠從這裏繼續
0
但誰創造這些事件? 我認爲這assosiation將是對你有好處:
models/event.rb
has_many :users, :dependent => :destroy
models/user.rb
belongs_to :event
而且用戶遷移應該看起來像這樣:
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :name
t.integer :event_id
t.timestamps
end
add_index :users, :event_id
end
end
事件遷移:
class CreateEvents < ActiveRecord::Migration
def change
create_table :events do |t|
t.string :name
t.timestamps
end
end
end
你可以閱讀有關軌道的詳細信息本指南中的聯想: http://guides.rubyonrails.org/association_basics.html
0
用戶模式
has_many :events, :dependent => :destroy
事件模型
belongs_to :user
事件遷移:
class CreateEvents < ActiveRecord::Migration
def change
create_table :events do |t|
t.string :name
t.references :user
t.boolean :participant, :default => true, :null => false # the user is participant by default set, default to true
t.timestamps
end
end
end
+0
我應該提到,用戶可以與事件沒有關係,不感興趣或參與者 – 2013-02-14 09:42:29
+0
是否像,用戶可以參與或對許多事件感興趣?那麼你需要用戶和事件之間的多對多關係 – shweta 2013-02-14 09:48:20
相關問題
- 1. 關聯Neo4j :: ActiveNode模型與ActiveRecord模型
- 2. ActiveRecord的模型「型」態關聯的列包含關聯的模型
- 3. ActiveRecord模型中的複雜關聯
- 4. ActiveRecord polymorpic模型關聯混淆
- 5. 基於關聯模型的數據驗證和ActiveRecord模型?
- 6. 將模型與2關聯關係node.js
- 7. 關聯模型
- 8. 如何從2個沒有任何關聯的ActiveRecord模型中獲取結果
- 9. 關聯模型
- 10. rails activerecord保存有唯一性驗證的關聯模型
- 11. ActiveRecord關聯
- 12. rails4 ActiveRecord關聯
- 13. ActiveRecord has_n關聯
- 14. ActiveRecord類關聯
- 15. Rails ActiveRecord關聯
- 16. Sum ActiveRecord關係(模型+模型=數組?)
- 17. 在ActiveRecord模型中添加關聯的好處?
- 18. ActiveRecord保存相關模型
- 19. ActiveRecord返回具有未初始化關聯的模型對象
- 20. 有兩個關聯模型的ActiveRecord查詢
- 21. 如何查找ActiveRecord模型對象的關聯Resque作業?
- 22. 如何配置與非ActiveRecord模型的has_many關聯
- 23. 驗證ActiveRecord中的關聯模型(has_many通過)
- 24. Rails ActiveRecord模型範圍與has_many關聯上的連接
- 25. ActiveRecord:來自關聯模型的參考屬性where語句
- 26. 與ActiveRecord相同模型的多對多關聯?
- 27. Rails3 - 如何獲取ActiveRecord模型的所有屬性和關聯?
- 28. 如何訪問ActiveRecord關聯方法中的原始模型
- 29. CakePHP模型關聯
- 30. 獲取Rails模型的關聯關聯
我應該如何區分**在**和**參與者**之間進行交流? – 2013-02-14 09:06:27