Relationships in models

I am new to rails and have got very confused with the above. It was
working and then I made changes and it has gone downhill from there.
And I was not taking regular backups…
I have moved slightly outside of the conventions but it is not unknown
for a record type to be refered in another record in several fields -
users being a good example.
My schema file.
create_table “messages”, :force => true do |t|
t.integer “from_user”, :null => false
t.integer “to_user”, :null => false
t.text “message”
t.datetime “created_at”
t.datetime “updated_at”
end

add_index “messages”, [“from_user”], :name =>
“fk_messages_from_users”
add_index “messages”, [“to_user”], :name => “fk_messages_to_users”

create_table “users”, :force => true do |t|
t.string “user_id”
t.string “first_name”
t.string “surname”
etc etc

My model files
class Message < ActiveRecord::Base
belongs_to :user_from,
:class_name => “User”,
:foreign_key => “from_user”

 belongs_to :user_to,
          :class_name => "User",
          :foreign_key => "to_user"

          validates_presence_of     :message

end
class User < ActiveRecord::Base
has_many :user_from,
:class_name => “Message”,
:foreign_key => “from_user”

 has_many :user_to,
          :class_name => "Message",
          :foreign_key => "to_user"

etc etc
I made changes to user looking at various google results. Previous I
simply had
class User < ActiveRecord::Base
has_many :messages
etc etc

The controller action.
def my_messages
user = User.find(session[:userid])
@first_name = user.first_name
@messages = Message.paginate(:page => params[:page], :per_page => 10,
:conditions => [“from_user = ? or to_user = ?”,
session[:userid],
session[:userid]],
:order => “created_at DESC”)
@message = Message.new(params[:message])
@message.from_user = user
@message.to_user = User.find(1)
if request.post? and @message.save
@message = Message.new()
@messages = Message.paginate(:page => params[:page], :per_page =>
10,
:conditions => [“from_user = ? or to_user = ?”, session
[:userid], session[:userid]],
:order => “created_at DESC”)
end

And this is where I get confused. if I change the two lines above
@message.from_user = user
@message.to_user = User.find(1) # I know its hard coded but that
is the least of my worries
to
@message.from_user = session[:userid]
@message.to_user = 1
then I get expected results (ie value is 6) but
message.from_user.first_name will give an undefined method error.
If I use the former code then the value of from_user is 1 rather than
6. I have not a clue where it gets 1 from.

So, in summary, I have not got this. Any help appreciated.

Regards,

john

Thank you for this. I agree with what you have said but could you give
an example of what you mean. For instance, are my model files correct?
Also the point I made about setting message.user_from to user then
gives a value of 1 rather than a value of 6 (the id of the user).

message.from_user.first_name will give an undefined method error.

message.from_user is an integer

message.user_from is a reference to a user, and user has a first_name
method

Now I get it.
The association name is the name used for the object. And I had the
field name and the association which I had not realised but a least it
emphasises the point.
And by assigning the object to the this, I can then display values for
the referred object. So my code is now
class Message < ActiveRecord::Base
belongs_to :user_from_obj,
:class_name => “User”,
:foreign_key => “from_user”
etc etc
user = User.find(session[:userid])
@message = Message.new(params[:message])
@message.user_from_obj = user
etc etc
many thanks .