views:

132

answers:

3

I want to model messages between users, here is the requirement:

  1. User has received and sent messages, and should be retrieved by user.received_messages and user.sent_messages

  2. Message has sender and receiver, and should be retrieved by message.sender and message.receiver.

I have created the User model as:

script/generate model User name:string

and Message model as:

script/generate model Message content:text sender_id:integer receiver_id:integer

I have come up with the Message like below, and it works as wish

class Message < ActiveRecord::Base
    belongs_to :sender, :class_name=>'User', :foreign_key=>'sender_id'
    belongs_to :receiver, :class_name=>'User', :foreign_key=>'receiver_id'
end

but I don't know how to model the User, any advise is appreciated.

+2  A: 

Just as in the Message model:

class User < ActiveRecord::Base
  has_many :sent_messages, :class_name => "Message", :foreign_key => "sender_id"
  has_many :received_messages, :class_name => "Message", :foreign_key => "receiver_id"
end
Milan Novota
A: 

I got this from the console.

>> me = User.find 1
ArgumentError: Unknown key(s): foriegn_key
    from /usr/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/active_support/core_ext/hash/keys.rb:49:in `assert_valid_keys'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/associations.rb:1170:in `create_has_many_reflection'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/associations.rb:667:in `has_many'
    from /home/hqi/rorlab/test/app/models/user.rb:3
    from /usr/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:203:in `load_without_new_constant_marking'
    from /usr/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:203:in `load_file'
    from /usr/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:342:in `new_constants_in'
    from /usr/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:202:in `load_file'
    from /usr/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:94:in `require_or_load'
    from /usr/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:248:in `load_missing_constant'
    from /usr/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:453:in `const_missing'
    from /usr/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:465:in `const_missing'
    from (irb):26

any ideas?

eric2323223
You’ve got a typo: 'foriegn_key'
Matt
+1  A: 

@eric2323223

If you just copy/pasted the code from Milan, then I suspect the error you're seeing is because there's a missing semi-colon in front of 'foreign_key'.

The lines should read:

  has_many :sent_messages, :class_name => "Message", :foreign_key => "sender_id"
  has_many :received_messages, :class_name => "Message", :foreign_key => "receiver_id"

Kenny

Kenny C
fixed the typo. thanks. +1 for a good spot ;)
Milan Novota
Thanks for the response, I made a typo "foriegn_key".
eric2323223