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

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to