find_or_create_by メソッドを使用して Client を追加しようとすると、trainer_id が値として渡されません (trainer_id for client = nil)
私のClient_Controllerにもビルドメソッドがあります。これは、current_trainerからIDを取得すると想定しています
find_or_create_by_name と build メソッドの間に切断があると想定しています。
これについて何か助けていただければ幸いです。
Client_Controller
def create
@client = current_trainer.clients.build(params[:client])
respond_to do |format|
if @client.save
ClientMailer.registration_confirmation(@client).deliver
format.html { redirect_to @client, :notice => 'Client was successfully added.' }
format.json { render :json => @client, :status => :created, :location => @client }
else
format.html { render :action => "new" }
format.json { render :json => @client.errors, :status => :unprocessable_entity }
end
end
end
ワークアウト モデル
class Workout < ActiveRecord::Base
belongs_to :client
belongs_to :trainer
validates_presence_of :day,
:client_id,
:trainer_id,
:message => "You have to indicated for when you want to schedule this workout."
def client_name
client.try(:name)
end
def client_name=(name)
self.client = Client.find_or_create_by_name(name) if name.present?
end
end
クライアント モデル
class Client < ActiveRecord::Base
belongs_to :trainer
before_save :generate_password
has_many :workouts
has_many :weigh_ins
validates_presence_of :name, :message => "You have to provide a client name in order to add new client."
has_attached_file :profile_pic, :styles => { :micro => "60x60>", :small => "150x150>" },
:default_url => 'profile_default_small.jpg',
:storage => :s3,
:s3_credentials => S3_CREDENTIALS,
:bucket => '#',
:path => ":attachment/:id/:style/:basename.:extension"
validates_attachment_size :profile_pic,
:less_than => 3.megabytes,
:message => "Your profile picture can`t be bigger than 3MB, sorry."
def generate_password
self.password = name[0..2].to_s + rand(99).to_s
end
def self.authenticate(email, password)
client = find_by_email(email)
if client && client.password == client.password
client
else
nil
end
end
サーバーログ
Started POST "/workouts" for 127.0.0.1 at Wed Apr 25 17:39:37 -0400 2012
Processing by WorkoutsController#create as HTML
Parameters: {"commit"=>"Schedule Session", "authenticity_token"=>"CxJSVfn0fwerdyrpA9/JEe8fX8Ep2/ZhnOqQkjZ3iwE=", "utf8"=>"✓", "workout"=>{"client_name"=>"John Doe", "day(1i)"=>"2012", "day(2i)"=>"4", "day(3i)"=>"29", "day(4i)"=>"21", "day(5i)"=>"00", "note"=>"", "title"=>"Current_Client"}}
Trainer Load (0.1ms) SELECT "trainers".* FROM "trainers" WHERE "trainers"."id" = ? LIMIT 1 [["id", 37]]
Client Load (0.2ms) SELECT "clients".* FROM "clients" WHERE "clients"."name" = 'John Doe' LIMIT 1
(0.0ms) begin transaction
SQL (0.4ms) INSERT INTO "clients" ("created_at", "email", "goal_weight", "name", "notes", "password", "profile_pic_content_type", "profile_pic_file_name", "profile_pic_file_size", "profile_pic_updated_at", "start_weight", "trainer_id", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) [["created_at", Wed, 25 Apr 2012 21:39:37 UTC +00:00], ["email", nil], ["goal_weight", nil], ["name", "John Doe"], ["notes", nil], ["password", "Joh55"], ["profile_pic_content_type", nil], ["profile_pic_file_name", nil], ["profile_pic_file_size", nil], ["profile_pic_updated_at", nil], ["start_weight", nil], ["trainer_id", nil], ["updated_at", Wed, 25 Apr 2012 21:39:37 UTC +00:00]]
[paperclip] Saving attachments.
(2.3ms) commit transaction
(0.1ms) begin transaction
SQL (0.5ms) INSERT INTO "workouts" ("client_id", "created_at", "day", "note", "title", "trainer_id", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?) [["client_id", 53], ["created_at", Wed, 25 Apr 2012 21:39:37 UTC +00:00], ["day", Sun, 29 Apr 2012 21:00:00 UTC +00:00], ["note", ""], ["title", "Current_Client"], ["trainer_id", 37], ["updated_at", Wed, 25 Apr 2012 21:39:37 UTC +00:00]]
(1.1ms) commit transaction
Redirected to http://localhost:3000/workouts/64
Completed 302 Found in 13ms (ActiveRecord: 4.8ms)