I have a create method in Rails where I am trying to create multiple objects in a while loop. For some reason it doesn't seem to be hitting the while loop so no objects are being created. The code is below:
def create
#user = User.find(params[:participant][:user_id])
#activity = Activity.find(params[:activity_id])
weeks = #activity.weeks
i = 1
while i <= weeks do
puts "Test"
participant = Participant.new
participant.user_id = #user.id
participant.activity_id = #activity.id
participant.attended = false
participant.paid = false
participant.week = i
participant.save
i = i+1
end
redirect_to user_activities_path(#user, :id => #activity.id)
end
The form I am using to submit is working fine as I can see from the console, and the redirect_to method at the end is working, so it just seems to be missing the loop. If it helps, the value of weeks is 10. Any help would be greatly appreciated.
If multiple Test have been output, try participant.save!, i think the participant save might fail, like some column not valid, so no objects are being created.
Please check if activity record is get fetched. I think your 3rd statement should be as follow.
#activity = Activity.find(params[:participant][:activity_id])
Related
This is my code, the first part is the problem, and the second is perfectly working.
def show
if #user.cooker?
#pushs = #user.pushs.where(taked_by = #user.id)
else
#pushs = #user.pushs
end
end
Well the code say, if I'm a cooker, I want to display all the push where the variable :taked_by is has the same id than my user.id
And the second par say that if I'm not a cooker, display all my own push.
Do taked_by: #user.id or :taked_by => #user.id
def show
if #user.cooker?
#pushs = #user.pushs.where(taked_by: #user.id)
else
#pushs = #user.pushs
end
end
Let me know if it worked.
I am working on a Rails app, and I am attempting to insert attributes from JSONs as database entries. I'm running into a problem, though, and would appreciate some guidance.
I've been able to jam a few things together and come up with something that sort of works...
def create
#report_group = Array.new
#report_group.push({location:"home", comments:"Hello, database!"}, {location:"away", comments:"Goodbye, database!"})
#report_group.each do |x|
#new_report = Report.new(x)
#new_report.user_id = current_user.id
#new_report.save
end
end
private
def report_params(params)
params.permit(:user_id,:location,:comments)
end
This is a good first step - this commits two entries to my database, one for each of the hashes pushed into #report_group, but it is suffering from a problem - the create action does not reference the report_params whitelist.
I have built several Rails apps where entries are submitted one at a time via the standard Rails form helpers, but I have never done it with multiple JSONs like this before. Trying out the syntax I'd use in a typical form helper situation
#new_report = Report.new(report_params(x))
throws the expectable error undefined method permit' for #<Hash:0x007f966b35e270> but I am not sure what else to do here.
EDIT TO SHOW SOLUTION
Big thanks to #oreoluwa for pointing me in the right direction. Here's the solution that I came up with.
def create
#report_group = Array.new
#report_group.push({location:"home", comments:"Hello, database!"}, {location:"away", comments:"Goodbye, database!"})
#report_group.each do |x|
hash = ActionController::Parameters.new(x)
#new_report = Report.new(report_params(hash))
#new_report.user_id = current_user.id
#new_report.save
end
end
private
def report_params(params)
params.permit(:user_id,:location,:comments)
end
You're getting the error because a Hash is not the same as an ActionController::Parameters. In order to use the permit method with your Hash you may need to first convert it to ActionController::Parameters, as such:
hash = {location:"home", comments:"Hello, database!"}
parameter = ActionController::Parameters.new(hash)
parameter.permit(:user_id,:location,:comments)
I don't know if that is what you're looking for, but I thought to point you in the right direction.
I need to append data to a variable
#celebrity = Celebrity.includes(:category).where('category_id = ?', params[:id])
test =[]
#celebrity.each do |celeb|
#vote = Vote.where('celebrity_id = ?', celeb).count
test << {vote_count:#vote}
end
when i debug 'test',
abort test.inspect
I am getting the result of
[{:vote_count=>2}, {:vote_count=>1}, {:vote_count=>0}]
but, my question is how can I append vote_count to #celebrity , can anyone please help me out
You should not do it this way, its terrible in terms of performance.
If you setup a counter_cache properly (see ref), you'd have data right away in your model instances as expected
#celebrity.each do |celeb|
celeb["vote_count"] = celeb.votes.count
end
+apneadiving is right. use counter_caches
I'm trying to delete a single instance from a database query. "l.remove" represents what i want to do but i know its wrong. I have tried delete and destroy. destroy didn't work and delete actually removed the data from the database. I just want the data removed from the variable. Can anyone help me?
<%
#owner = User.find(params[:id])
#job_list = ShoppingList.where(:user_id=>#user.user_id)
#job_list.each do |l|
#temp = FlaggedCandidate.where(:flagged_user_id=>#owner.user_id, :list_id=>l.list_id)
if !#temp.nil?
l.remove
end
end
#candidate = FlaggedCandidate.new
%>
based on the code i assume that User has many ShoppingList.
You can do something like:
#job_list = #owner.shopping_lists.where( list_id: FlaggedCandidate.where( flagged_user_id: #owner.user_id ).pluck(:list_id) )
That could save the trouble of looping around.
You are trying to remove record from db. In order to just modify collection #job_list you need reject some unsatisfied elements. You can do it with select method (to select job_lists that flagged), or reject in opposite. This is how you code should looks like:
#owner = User.find(params[:id])
#job_list = ShoppingList.where(:user_id=>#user.user_id)
#job_list.select! do |job_list|
FlaggedCandidate.where(
:flagged_user_id => #owner.user_id,
:list_id => job_list.list_id
).any?
end
#candidate = FlaggedCandidate.new
select! simply change the original collection, instead of doing #job_list = #job_list.select { ... }
I am using factory girl to create a model with inventory_count = 3. In my test, I want to test a case for when inventory_count = 0..so here's what I did:
before(:each) do
#user = FactoryGirl.create(:user)
#piece = FactoryGirl.create(:piece)
#lineup = #user.lineup
end
it 'should have status \'Waiting List\' if the piece doesn\'t have available inventory' do
#piece.available_count = 0
#lineup.pieces << #piece
piece_lineup = #lineup.piece_lineups.find_by_piece_id(#piece.id)
piece_lineup.set_status
piece_lineup.status.should == 'Waiting List'
end
I put a debugger after #piece.available_count = 0 and it is = 0, but when it gets down to the next line it switches back to the old value. I tried adding a .save to #piece but it still didn't work. Am i doing something wrong? Should I be creating the new value model in factorygirl instead of trying to do it on the fly?
I believe it would be better practice to write:
#piece = FactoryGirl.create(:piece, :available_count => 0)
to create a piece model with its count already 0.
Also, you're question begins mentioning inventory_count. Did you actually mean available_count? Just want to make sure you didn't have a typo.
The change isn't making it to the DB. Try
#piece.update_attributes!(available_count: 0)
EDIT: To skip the callback that is resetting the available_count, use update_column(:available_count, 0)