Re: Multiple relations between two models

Stewart
Thu, 19 Oct 2006 14:37:22 -0000

Thorsten L wrote:
> i think you got something twisted up.
> -> in the model, you say, the sender''s foreign key is
"user_id"
> -> in the controller, you assign the recipient ( params[:message][:to])
> to the column "user_id"
>
>>From what you described i guess you have a foreign_key
"sender" in your
> message table that shold hold the ID of the user who sent the message,
> and the "user_id" filed should hold the name of the recipient of
the
> message
> Your column naming could be more self-explanatory, e.g. use
> "recipient_id" as i suggested in my above post
>
> i think, your :belongs_to sould look like this:
>
> belongs_to :sender, :class_name "User", :foreign_key =>
"sender"
> belongs_to :recipient, :class_name "User", :foreign_key =>
"user_id"
>
> but you should give some more info, like your exact table layout etc
> ...
> and what do the parameters hold? i guess params[:message][:to] hold the
> recipients name. well you need that user''s id, not his name.
>
> But im just guessing here, not enough insight and info....

Ok sorry i should have posted this stuff beofre... here we go

class CreateUsers < ActiveRecord::Migration
  def self.up
    create_table "users", :force => true do |t|
      t.column :login,                     :string
      t.column :email,                     :string
      t.column :crypted_password,          :string, :limit => 40
      t.column :salt,                      :string, :limit => 40
      t.column :created_at,                :datetime
      t.column :updated_at,                :datetime
      t.column :remember_token,            :string
      t.column :remember_token_expires_at, :datetime
      t.column :first_name, :string, :null => false
      t.column :last_name, :string, :null => false
      t.column :organization_id, :integer, :null => false
      t.column :title, :string
      t.column :location, :string
    end
  end

  def self.down
    drop_table "users"
  end
end

class CreateMessages < ActiveRecord::Migration
  def self.up
    create_table :messages do |t|
      t.column :title, :string, :null => false
      t.column :body, :text, :null => false
      t.column :user_id, :integer, :null => false
      t.column :sender, :integer, :null => false
      t.column :created_at, :datetime, :null => false
      t.column :read_at, :datetime

    end
  end

  def self.down
    drop_table :messages
  end
end


the sender column in the message table stores the user_id of the user
sending the message

the user_id in the message table contains the id number of the user that
is the recipient (I will later change this to recipient_id its a better
name)

params[:message][:to] is the id of the user that the message is being
set to. The code looks like this...

To
<%= collection_select("message", "to" ,
current_user.organization.users,
"id", "full_alpha_name" ) %>


its all working now though i just needed to fix up my keys and add in
that other belongs_to..

Thanks soo much for that guys!