我有一个包含活动,用户,会谈和通知的应用程序。模型包括
用户模型
has_many :talks, :dependent => :destroy
has_many :notifications, :dependent => :destroy
has_many :activities, :dependent => :destroy
通知模型
has_many :users, -> {uniq}, through: :talks
belongs_to :user
belongs_to :trackable, polymorphic: true
has_many :talks
has_many :notifications
创建谈话时我会这样做
def create
@activity = Activity.find(params[:activity_id])
@talk = @activity.talks.create!(talk_params)
@talk.user = current_user
@talk.save
#@users= @activity.users.where("id NOT IN (?)", [@activity.user.id, @talk.user])
@users= User.joins(:talks).where(talks: {id: @activity.talk_ids}).push(@activity.user).reject {|user| user == @talk.user }.uniq
## Lets create a notification for all those who created a comment in this activity
@users.each do |user|
Notification.create(activity:@activity, user: user)
end
## Lets create a notification for the owner activity
#Notification.create(activity:@activity, user: @activity.user)
redirect_to user_path(current_user)
end
并且通过此创建通知我想要的是显示通知所有者的名称..我不知道如何去做那个
答案 0 :(得分:0)
试试这段代码:
@users.each do |user|
noti = Notification.create(activity:@activity, user: user)
notifications = @activity.user.notifications
notifications << noti
@activity.user.update(notifications:, notifications)
end
如果每个通知都有创建者(:用户),接收者(:用户),那么您需要有两列通知模型(sender_id,recipient_id)。
您需要阅读有关单表(User)
的继承Inheritance from single table(1) Inheritance from single table(2)