2015-06-30 87 views
0

如何保存屬性..我有一個非常簡單的問題..這裏我嘗試保存屬性的新對象像在因爲即時通訊新軌軌

 @receiver = User.find_by_email(params[:email]) 
     @sender = User.find_by_email(current_user.email) 

     logger.info "@receiver.inspect"[email protected] 
     logger.info "@sender.inspect"[email protected] 

     @newConnection = Connection.new 
     @newConnection.user1 = @receiver.id 
     @newConnection.user2 = @sender.id 


     if @newConnection.save 
     flash[:message] = "Saved Succesfully!" 

,但它的作用只是保存的默認領域,而不是接收者和發送者ID爲日誌

 INSERT INTO `connections` (`created_at`, `updated_at`) VALUES ('2015-06-30 20:14:30', '2015-06-30 20:14:30') 

我的連接模型就像

 class Connection < ActiveRecord::Base 
       attr_accessor :user1,:user2 
       belongs_to :user1, :class_name => "User" 
       belongs_to :user2, :class_name => "User" 
     end 

模型M移徙

  class CreateConnections < ActiveRecord::Migration 
     def change 
create_table :connections do |t| 
    t.integer :user1_id, :references => [:users, :id] 
    t.integer :user2_id, :references => [:users, :id] 
    t.date :connectedOn 
    t.string :currentStatus 

    t.timestamps 
     end 
    end 
    end 

請幫忙!

+0

向您的連接控制器顯示強參數的定義。 – uday

回答

1

你應該分配用戶對象,但不是它的id這樣的:

@newConnection.user1 = @receiver 
@newConnection.user2 = @sender 

而且最重要的是從連接模式刪除attr_accessor中所以這將是這樣的:

class Connection < ActiveRecord::Base 
    belongs_to :user1, :class_name => "User" 
    belongs_to :user2, :class_name => "User" 
end 

我還將user1和user2命名爲發件人和收件人或其他更有意義的名稱。