views:

73

answers:

1

Hi, Is it possible to set up a double relationship in a Rails scaffold?

For example, if I had a user model and a private message model, the pm table would need to keep track of both the sender and recipient.

Obviously, for a single relationship I would just do this:

ruby script/generate scaffold pm title:string content:string user:references

Is there a similar way to set up two relations?

Also, is there anyway to set up aliases for the relations?

So rather than saying:

@message.user

You can use something like:

@message.sender or @message.recipient

Any advice would be greatly appreciated.

Thanks.

+4  A: 

Add this to your Model

has_one :sender, :class_name => "User"
has_one :recipient, :class_name => "User"

And you are able to call @message.sender and @message.recipient and both reference to the User model.

Instead of user:reference in your generate command you'd need sender:reference and recipient:reference

Veger