Open Woody88 opened 9 years ago
where does this code exist? in which controller or which model ?
In the PostedShift controller so when some clicks on the link pick_up thats when the trade it triggered.
but i modify it to this(see below) class PostedShiftsController < ApplicationController
def pick_up
@buyer = Profile.find_by(:user_id => current_user.id)
@shift = Shift.find_by(:shift_id => params[:sid], :current_owner => params[:uid] )
@bshift = @buyer.shifts.find_by(:date => @shift.date)
if @bshift.nil? or !@bshift.overlaps?(@shift)
@trade = Shift.confirm_trade(@shift)
@trade.current_owner = current_user.id
@trade.shift_posted = "Not Posted"
if @trade.save
@shift.destroy
end
respond_with(@trade, :location => shifts_url)
end
end
end
In the Shift model I added this
class Shift < ActiveRecord::Base
def self.confirm_trade(seller)
attrs = ["position", "date", "start_time", "finish_time", "original_owner"]
self.new(seller.attributes_and_values(attrs))
end
def attributes_and_values(array)
result = Hash.new
array.each do |name|
result[name] = self.attributes[name]
end
result
end
end
I'm not sure if its the correct way to do things, So its supposed to copy the record of the seller into the buyer and delete the record of the seller.
why not just change the shift's user_id
?
Mohammed omg... I think that I was just thinking just way too complicated.. anyhow I have made the change by using user_id. And of course it works. But there is a problem with shift controller. This code runs perfectly until this part '@shift.shift_posted = "Posted" ' for some reason it does not make that change..
def create
@user = Profile.find(current_user.id)
@shift = @user.shifts.find_by(params[:id])
@posted_shift = PostedShift.new(posted_shift_params)
@posted_shift.seller_id = current_user.id
@posted_shift.st_id = @shift.id
@shift.shift_posted = "Posted" #right here its as if it completely ignores it.
if @posted_shift.save && @shift.save
flash[:notice] = "Shift Posted For Trade"
end
respond_with(@posted_shift, :location => @posted_shifts_path)
end
Having problem picturing the structure of my shift model. I want the user to be able to trade or swap shift through the PostedShift controller or send a direct message that request a trade. But since I dont have messages system integrated yet, I will just stick with trade that can be done through PostedShift controller. So I'm thinking of something of this sort(see below) but I heard alot about having slim controller and fat model. Is this logic correct or good practice?