2011-04-16 30 views
0

我有一個模型,Tran具有用戶模型的外鍵。在創建Tran(事務)的視圖中,我有一個允許用戶選擇啓動事務的用戶的下拉列表。當我張貼此交易中,記錄設定正確的用戶ID:Rails - 正在提交已經存在的嵌套值

然後,在我的跨模型我補充說:「belongs_to的」,因爲我明白我應該爲外鍵做到這一點:

class Tran < ActiveRecord::Base 
    belongs_to :buying_user, :class_name => 'User' 

現在,當我的客戶在帖子中傳遞參數時,我的Tran.new瘋狂出來,因爲我傳遞了一個用戶ID而不是完整的記錄。是

#trans_controller.rb 
def create 
    @title = "Create Transaction" 
     #bombs on this call 
    @tran = Tran.new(params[:tran]) 

我該如何處理這個?根據要求

更新: tran.rb

class Tran < ActiveRecord::Base 
    has_many :transaction_users, :dependent => :destroy, :class_name => 'TransactionUser' 
    belongs_to :submitting_user, :class_name => 'User' 
    belongs_to :buying_user, :class_name => 'User' 

    accepts_nested_attributes_for :transaction_users, :reject_if => lambda { |a| a[:content].blank? }, :allow_destroy => true 

    validates :description, :presence => true, 
          :length => {:maximum => 100 } 
    validates :total,  :presence => true 
    validates_numericality_of :total, :greater_than => 0 

    validates :submitting_user,   :presence => true   
    validates :buying_user,    :presence => true 

    validates_associated :transaction_users 

end 

user.rb

class User < ActiveRecord::Base 
    has_many :trans 
    attr_accessor :password 
    attr_accessible :firstname, :lastname, :email, :password, :password_confirmation 

    email_regex = /\A[\w+\-.][email protected][a-z\d\-.]+\.[a-z]+\z/i 


    validates :firstname, :presence => true, 
          :length => {:maximum => 50 } 
    validates :lastname, :presence => true, 
          :length => {:maximum => 50 } 
    validates :email, :presence => true, 
          :format => {:with => email_regex }, 
          :uniqueness => { :case_sensitive => false } 

    validates :password, :presence => true, 
          :confirmation => true, 
          :length => { :within => 6..40 } 

    # Register callback to before save so that we can call extra code like password encryption      
    before_save :encrypt_password 

    # Class methods 
    def self.authenticate(email, submitted_password) 
     user = find_by_email(email) 
     return nil if user.nil? 
     return user if user.has_password?(submitted_password) 
    end 

    def self.authenticate_with_salt(id, cookie_salt) 
     user = find_by_id(id) 
     (user && user.salt == cookie_salt) ? user : nil 
    end 

    # Public methods 

    def has_password?(submitted_password) 
     self.encrypted_password == encrypt(submitted_password) 
    end 

    def full_name 
     "#{self.lastname}, #{self.firstname}" 
    end 

    def self.active_users 
     # TODO 
     #User.find_ 
     User.all 

    end 


    private 

     def encrypt_password 
      self.salt = make_salt if new_record? 
      self.encrypted_password = encrypt(password) 
     end 

     def encrypt (string) 
      secure_hash("#{salt}--#{string}") 
     end 

     def make_salt 
      secure_hash("#{Time.now.utc}--#{password}") 
     end 

     def secure_hash(string) 
      Digest::SHA2.hexdigest(string) 
     end 
end 

PARAMS散列上提交:

{"commit"=>"Submit", 
"tran"=>{"total"=>"100", 
"submitting_user"=>"1", 
"description"=>"Description"}, 
"authenticity_token"=>"88qI+iqF92fo/M9rPfMs1CLpEXqFLGQXfj0c9krXXac=", 
"utf8"=>"âœ「", 
"user"=>"1"} 

錯誤:

User(#70040336455300) expected, got String(#70040382612480) 

開始控制器:

def create 
    @title = "Create Transaction" 
    @tran = Tran.new(params[:tran]) 

它崩潰的Tran.new線。非常感謝!

回答

1

我想通了!整個過程中的問題是,我的Trans表中鏈接到我的Users表的db列名是submitting_user而不是submitting_user_id。然後,當我將belongs_to關聯添加到submitting_user時,導軌感到困惑,並將該字段設置爲User,而不是整數。

1

通常情況下,用戶模型將有has_many :transactions, :class_name => Tran 然後你可以這樣做......

@user.transaction_create(params[:tran]) 
or 
@user.build 

要看是什麼參數params中實際傳遞[:TRAN],但想法是的has_many側是否創建了belongs_to。

+0

@Jim Morris - 我的表中實際上有兩個用戶具有這樣的FK,所以我不能只調用其中一個的transaction_create。 – skaz 2011-04-16 23:51:56

+0

你的意思是Tran屬於兩個不同的用戶?在這種情況下,它不能是一個belongs_to,而是一個has_and_belongs_to_many – 2011-04-17 00:07:28

+0

@Jim Morris - 是的,Trans有一個SubmittingUser和一個BuyingUser。另外,我認爲我真正的問題是我使用的belongs_to沒有默認名稱。如果你注意到上面我有「belongs_to:submitting_user」,但它引用的模型是user,而不是submitting_user。我認爲這應該會爲我生成一個submitting_user_id,但它似乎並未創建。 – skaz 2011-04-17 00:12:15