I’m new to this. I have a (sqlite3, but with ActiveRecord it doesn’t matter) table called Messages and a model called Message. I want to find all messages in database that have user_id or reciever_id equal to the object user and his attribute id (for short user.id). I know it’s probably just one simple line of code, but I wanna do it the right “rails” way and I don’t have much experience with this.
I’m using Rails 3. Thanks for any help.
Cheers
I suspect that what you will want this relationship in many places in your code, and actually this represents a fundamental part of your application’s design.
Conceptually a ‘message’ belongs to a ‘sender’ and also to a ‘receiver’. In reverse, a ‘user’ has many messages that she has sent, and many messages that she has received.
in the Message model, add the following
in the User model, add the following
Now you can do this: