Domanda

I'm trying to set up a notification system in Rails, along with mongoid (but I don't think this is mongoid specific).

The basic structure is like this - Every notification has a notifier (the one who is responsible for the notification) and a notifee (the one who receives the notification). When a user A comments on user B's post (in a blog system for example), the User A becomes the notifier and the User B is the notifiee.

User.rb

# nothing in here

Notification.rb

has_one :notifier, :class_name => "User"
belongs_to :notifiee, :class_name => "User"

However, when I do:

@notification = Notification.new
@notification.notifier = current_user
@notification.notifiee = User.first #Just for the sake of the example
@notification.save

I get this error:

Problem: When adding a(n) User to Notification#notifier, Mongoid could not determine the inverse foreign key to set. The attempted key was 'notifiee_id'.Summary: When adding a document to a relation, Mongoid attempts to link the newly added document to the base of the relation in memory, as well as set the foreign key to link them on the database side. In this case Mongoid could not determine what the inverse foreign key was.Resolution: If an inverse is not required, like a belongs_to or has_and_belongs_to_many, ensure that :inverse_of => nil is set on the relation. If the inverse is needed, most likely the inverse cannot be figured out from the names of the relations and you will need to explicitly tell Mongoid on the relation what the inverse is.

What could I be doing wrong? Or, is there a better way to model this??

Any help is much appreciated! Thank you.

È stato utile?

Soluzione

You should probably go for the following associations:

User:

has_many :notifications_as_notifier, :class_name=>'Notification', :foreign_key=>'notifier_id'
has_many :notifications_as_notifiee, :class_name=>'Notification', :foreign_key=>'notifiee_id'

Notification:

belongs_to :notifier, :class_name=>'User', :foreign_key=>'notifier_id'
belongs_to :notifiee, :class_name=>'User', :foreign_key=>'notifiee_id'

Your notifications table should have notifier_id and notifiee_id.

Now you can do,

@notification = Notification.new
@notification.notifier = current_user
@notification.notifiee = User.first #Just for the sake of the example
@notification.save

What I find questionable in your setup:

You have,

has_one :notifier, :class_name => "User"
belongs_to :notifiee, :class_name => "User"

When you use has_on, then the other relation (table) must have a foreign key referencing the parent. Here users must have a column notification_id or something. This is impractical because a single user has many notifications (based on your explanations).

Secondly, you are associating Notification to User through two relationships but you are mentioning anything about the foreign key to use to enforce the association.

And why do you not have an inverse relation in the User model? Would it not help if you had access to something like: current_user.notifications_as_notifier ??

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top