我有一个带路线的控制器" class"这里:
def class
params[:parents].each do |rel|
@object = Message.new({text:params[:text]})
@object.to_account = Account.find(rel[:account_id])
@object.from_account = Account.find(params[:from_account_id])
@object.save
end
end
我正在传递这个对象参数:
{
"from_account_id": 26,
"text": "Labor Day is coming up!",
"parents": [
{
"id": 6,
"account_id": 25,
"first_name": "Bob",
"last_name": "Barker",
"work_phone": null,
"work_zip": "29464",
"created_at": "2015-05-22T13:05:49.320Z",
"updated_at": "2015-05-22T13:05:49.320Z"
},
{
"id": 7,
"account_id": 26,
"first_name": "Jill",
"last_name": "Scott",
"created_at": "2015-05-22T13:05:49.320Z",
"updated_at": "2015-05-22T13:05:49.320Z"
}
]
}
当我运行它并将数据发送到服务器时,我得到了一个无尽的日志
CACHE(0.0ms)SELECT" accounts"。* FROM" accounts"在哪里"帐户"。" id" = $ 1 LIMIT 1 [[" id",25]] CACHE(0.0ms)SELECT" accounts"。* FROM" accounts"在哪里"帐户"。" id" = $ 1 LIMIT 1 [[" id",25]] CACHE(0.0ms)SELECT" accounts"。* FROM" accounts"在哪里"帐户"。" id" IS NULL LIMIT 1 CACHE(0.0ms)SELECT" accounts"。* FROM" accounts"在哪里"帐户"。" id" IS NULL LIMIT 1 (0.2ms)BEGIN (0.2ms)BEGIN (0.2ms)ROLLBACK (0.2ms)ROLLBACK
我不确定我做错了什么。
以下是模型:
# == Schema Information
#
# Table name: messages
#
# id :integer not null, primary key
# from_account_id :integer
# to_account_id :integer
# read_on :datetime
# text :text
# created_at :datetime not null
# updated_at :datetime not null
#
class Message < BaseModel
attr_protected
validates :to_account, :presence => true
validates :from_account, :presence => true
belongs_to :to_account, :class_name => "Account"
belongs_to :from_account, :class_name => "Account"
validates :text, :presence => true
end
class Account < ActiveRecord::Base
attr_accessor :password
attr_accessible :email, :password, :password_confirmation
#validation
email_reg_ex = /\A[\w+\-.]+@[a-z\d.]+\.[a-z]+\z/i
validates :email, :presence => true,
:format => { :with => email_reg_ex},
:uniqueness => { :case_sensitive => false}
validates :password, :presence => true,
:confirmation => true,
:length => { :within => 6..40 }
before_save :encrypt_password
has_one :token
has_one :parent_user
#message relationships
has_many :sent_messages,:foreign_key => :from_account_id , class_name: 'Message'#, -> { order('created_at DESC')}
has_many :received_messages,:foreign_key => :to_account_id , class_name: 'Message'#, -> { order('created_at DESC')}
has_many :new_messages, -> { where read_on: nil}, :foreign_key => :to_account_id , class_name: 'Message'#, -> { where read_on: nil }
## or class << self
## def authenticate(
def Account.authenticate(email, submitted_password)
user = Account.find_by_email(email);
(user && user.has_password?(submitted_password)) ? user : nil
end
def Account.authenticate_with_salt(id, cookie_salt)
user = find_by_id(id)
(user && user.salt == cookie_salt) ? user : nil
end
def has_password?(submitted_password)
encrypted_password == encrypt(submitted_password)
end
def activate (key)
if key == salt
self.update_attribute( :activated, true)
end
end
def name
self.email
end
end
# == Schema Information
#
# Table name: accounts
#
# id :integer not null, primary key
# email :string(255)
# created_at :datetime
# updated_at :datetime
# encrypted_password :string(255)
# salt :string(255)
# admin :boolean default(FALSE)
# activated :boolean default(FALSE)
# recover_password :boolean default(FALSE)
#
答案 0 :(得分:0)
看起来只是一个错字
params
{
"from_account_id": 26,
但
@object.from_account = Account.find(params[:from_account])
尝试将此行更改为
@object.from_account = Account.find(params[:from_account_id])