【发布时间】:2011-10-28 01:35:11
【问题描述】:
这有点复杂,我不知道如何实现它。我有一个用户模型和一个关系模型。用户可以互相“关注”(就像推特一样)。关系模型都设置正确并且效果很好。
接下来,我有一个事件模型。每个用户都有_and_belongs_to_many 事件(用户和事件之间的多对多关联)。用户“参加”活动。
我想做的是拉出所有事件的列表
- 由 current_user 参与
- current_user 正在关注的用户正在参加。
如果可能的话,我想通过 User 模型访问这个列表,这样我就可以说 current_user.event_feed ,它会列出上面提到的所有事件。
这是我的模型:
class Event < ActiveRecord::Base
attr_accessible :name,
:description,
:event_date,
:location,
:owner_id,
:category,
:photo
CATEGORIES = ['Music', 'Outdoors', 'Party']
has_and_belongs_to_many :users
和关系模型:
class Relationship < ActiveRecord::Base
attr_accessible :followed_id
belongs_to :follower, :class_name => "User"
belongs_to :followed, :class_name => "User"
validates :follower_id, :presence => true
validates :followed_id, :presence => true
end
和用户模型:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
attr_accessible :email, :password, :password_confirmation, :remember_me
attr_accessor :password
attr_accessible :name, :email, :password, :password_confirmation, :time_zone
has_and_belongs_to_many :events
has_many :relationships, :dependent => :destroy,
:foreign_key => "follower_id"
has_many :reverse_relationships, :dependent => :destroy,
:foreign_key => "followed_id",
:class_name => "Relationship"
has_many :following, :through => :relationships,
:source => :followed
has_many :followers, :through => :reverse_relationships,
:source => :follower
谢谢!
【问题讨论】:
标签: ruby-on-rails activerecord