Rails 4: Iterate through an array to create model in controller - ruby-on-rails

I pass parameters from a view to a controller. The parameter is an array that is generated by the user. The user can add as many items to the array as they want. I want to iterate through this array to create multiple model objects in the DB. How can I go about doing this?
A person can create a meal, and within the meal form, there are options to add as many food items as they wish.
def create
#meal= Meal.new(question_params)
food_options = params[:food_options]
i = 0
if #meal.save
food_options.each do |x|
#meal.foods.Create(:drink => food_option[i], :meal => #meal)
i = +1
end
redirect_to #meal
else
render 'new'
end
end
Any guidance would be much appreciated

Use accepts_nested_attributes_for and let Rails handle it for you.
In the models/meal.rb
class Meal < ActiveRecord::Base
has_many :foods
accepts_nested_attributes_for :foods # <==========
...
end
and in the controller, include the nested attributes:
class MealsController < ApplicationController
...
def create
#meal= Meal.new(question_params)
redirect_to #meals
else
render 'new'
end
...
def question_params
params.require(:meal).permit(...., foods_attributes: [ :drink, .... ]) # <====
end
end

Related

Rails to Render JSON with Newly Calculated Value for Each Element of a Rendered Collection

I want to add a newly calculated value to each element of a collection that is rendered as render json: when a Rails API route is queried.
This is the current set up:
# app/Models
class User < ApplicationRecord
has_many :pools, through: :user_pool
end
class Pool < ApplicationRecord
has_many :users, through: :user_pool
has_many :stakes
def average()
some_kind_of_average(self.stakes)
end
end
# app/controllers/pool_controller.rb
def index
#user = current_user
#pools = #user.pools
render json: #pools, only: [:ticker, :id]
end
What I would like to do is to add for each pool of the #pools collection a value of pool.average.
it should be something like this:
# app/controllers/pool_controller.rb
def index
#user = current_user
#pools = #user.pools
render json: #pools, only: [:ticker, :id, average: #pools.each {|pool| pool.average}]
end
Of course the above doesn't work, what is the most elegant way to achieve my goal?
The only way I can think of is to calculate all of the average prior queries, and just insert it in the database as a new attribute of Pool, so that the controller would simply become:
# app/controllers/pool_controller.rb
def index
#user = current_user
#pools = #user.pools
render json: #pools, only: [:ticker, :id, :average]
end
But I am wondering how would I do it if I want to avoid a mass calculation to update the database adding and :average attribute to every pool. Even though perhaps is the best solution rather than calculating averages for each query?
try this in pool.rb write a method called average and in that method write the logic,
in the index method of controller write this way
render json: #pools, only: [:ticker, :id, :average], methods: :average

ActiveRecord::RecordNotFound - in a descendant class' associated_controller#index

I am attempting to locate a parent object in a nested controller, so that I can associate the descendant resource with the parent like so:
# teams_controller.rb <snippet only>
def index
#university = Univeresity.find(params[:university_id])
#teams = #university.teams
end
When I call find(params[:university_id]) per the snippet above & in line 6 of teams_controller.rb, I receive ActiveRecord::RecordNotFound - Couldn't find University without an ID.
I'm not only interested in fixing this issue, but would also enjoy a better understanding of finding objects without having to enter a University.find(1) value, since I grant Admin the privilege of adding universities.
The Rails Guides say the following about the two kinds of parameters in a website:
3 Parameters
You will probably want to access data sent in by the user or other
parameters in your controller actions. There are two kinds of
parameters possible in a web application. The first are parameters
that are sent as part of the URL, called query string parameters. The
query string is everything after “?” in the URL. The second type of
parameter is usually referred to as POST data. This information
usually comes from an HTML form which has been filled in by the user.
It’s called POST data because it can only be sent as part of an HTTP
POST request. Rails does not make any distinction between query string
parameters and POST parameters, and both are available in the params
hash in your controller:
It continues a little further down, explaining that the params hash is an instance of HashWithIndifferentAccess, which allows usage of both symbols and strings interchangeably for the keys.
From what I read above, my understanding is that Rails recognizes both parameters (URL & POST) and stores them in the same hash (params).
Can I pass the params hash into a find method in any controller action, or just the create/update actions? I'd also be interested in finding a readable/viewable resource to understand the update_attributes method thats called in a controller's 'update' action.
Please overlook the commented out code, as I am actively searching for answers as well.
Thanks in advance.
Here are the associated files and server log.
Webrick
teams_controller.rb
class TeamsController < ApplicationController
# before_filter :get_university
# before_filter :get_team
def index
#university = University.find(params[:univeristy_id])
#teams = #university.teams
end
def new
#university = University.find(params[:university_id])
#team = #university.teams.build
end
def create
#university = University.find(params[:university_id])
#team = #university.teams.build(params[:team])
if #team.save
redirect_to [#university, #team], success: 'Team created!'
else
render :new, error: 'There was an error processing your team'
end
end
def show
#university = University.find(params[:university_id])
#team = #university.teams.find(params[:id])
end
def edit
#university = University.find(params[:university_id])
#team = #university.teams.find(params[:id])
end
def update
#university = University.find(params[:university_id])
#team = #university.teams.find(params[:id])
if #team.update_attributes(params[:team])
redirect_to([#university, #team], success: 'Team successfully updated')
else
render(:edit, error: 'There was an error updating your team')
end
end
def destroy
#university = University.find(params[:university_id])
#team = #university.teams.find(params[:id])
#team.destroy
redirect_to university_teams_path(#university)
end
private
def get_university
#university = University.find(params[:university_id]) # can't find object without id
end
def get_team
#team = #university.teams.find(params[:id])
end
end
team.rb
class Team < ActiveRecord::Base
attr_accessible :name, :sport_type, :university_id
has_many :home_events, foreign_key: :home_team_id, class_name: 'Event'
has_many :away_events, foreign_key: :away_team_id, class_name: 'Event'
has_many :medias, as: :mediable
belongs_to :university
validates_presence_of :name, :sport_type
# scope :by_university, ->(university_id) { where(team_id: team_id).order(name: name) }
# scope :find_team, -> { Team.find_by id: id }
# scope :by_sport_type, ->(sport_type) { Team.where(sport_type: sport_type) }
# scope :with_university, joins: :teams
# def self.by_university(university_id)
# University.where(id: 1)
# University.joins(:teams).where(teams: { name: name })
# end
def self.by_university
University.where(university_id: university_id).first
end
def self.university_join
University.joins(:teams)
end
def self.by_sport_type(sport_type)
where(sport_type: sport_type)
end
def self.baseball
by_sport_type('Baseball/Softball')
end
end
university.rb
class University < ActiveRecord::Base
attr_accessible :address, :city, :name, :state, :url, :zip
has_many :teams, dependent: :destroy
validates :zip, presence: true, format: { with: /\A\d{5}(-\d+)?\z/ },
length: { minimum: 5 }
validates_presence_of :name, :address, :city, :state, :url
scope :universities, -> { University.order(name: 'ASC') }
# scope :by_teams, ->(university_id) { Team.find_by_university_id(university_id) }
# scope :team_by_university, ->(team_id) { where(team_id: team_id).order(name: name)}
def sport_type
team.sport_type
end
end
views/teams/index.html.erb
Placed in gists for formatting reasons
rake routes output: (in a public gist)
enter link description here
rails console
You're not going to want to have both:
resources :universities #lose this one
resources :universities do
resources :teams
end
As for params... you have to give a param. So, when you go to http://localhost:3000/teams there are no params, by default. If you go to http://localhost:3000/teams/3 then params[:id] = 3 and this will pull up your third team.
Keep in mind the nomenclature of an index. The index action of Teams, is going to list all of the teams. All of them. There is no one University there, so what are you actually trying to find? If anything, you'd have, for your University controller:
def show
#university = University.find(params[:id])
#teams = #university.teams
end
so, the address bar will be showing http://localhost:3000/universities/23, right? params[:id] = 23, then you can find the teams associated with that university.

Changing a field in the users table after_create of a record satisfies a condition

I want to change a users workout_id in the Users table once he completes his workout. I can't figure out how to do this. Below are the two ways I've tried to figure this out.
Attempt 1: Put an if statement in the controller, that when true increments the users workout_id - The problem is the if statement only passes when the condition is already true, so the user needs to try to complete an additional exercise after his workout is already complete.
Example:
def create
#completed_set = CompletedSet.new(params[:completed_set])
if #totalExercisesInWorkout.included_in?(#completedExercises) == true
raise "it worked!"
end
respond_to do |format|
if #completed_set.save
format.html { redirect_to profile_path, notice: 'Successfully completed set.' }
format.json { render json: #completed_set, status: :created, location: #completed_set }
else
format.html { render action: "new" }
format.json { render json: #completed_set.errors, status: :unprocessable_entity }
end
end
end
Attempt 2 use the after_save callback in the Model. - this doesn't work because I don't have access to the session information in the Model, so I can't know what the user is, what their workout_id is, and if they have finished their workout.
There must be a simple solution to this problem, but I'm totally stumped!
Thanks!
EDIT:
A workout is complete when an array with the exercise_id's of the workout is matched by an array of the completed_set exercise_is's of the user. So when the following code is true:
#totalExercisesInWorkout.included_in?(#completedExercises)
.include_in? is an extension of the Array Class that simply returns true if the second array includes all the values in the first array.
A workout has_many exercises
When a user "completes" an exercise, that exercise and relevant information, such as user_id, workout_id, etc. is stored in the completed_set table.
In my application_controller I have an initialize_vars method that finds all the information I need to know about the user and their workouts. Here it is:
def initialize_vars
#workout = Workout.find(current_user.workout_id)
#user_program = current_user.program
#user_cycle = Cycle.find(current_user.cycle_id)
#user_group = Group.find(current_user.group_id)
#exercise = #workout.exercises
#workout_exercise = #workout.workout_exercises
# Array needs to have an exercise for every set in the exercise
#exercisesInWorkout = #workout.exercises.pluck(:exercise_id)
# Array of the sets in the users current workout
#workoutSets = #workout_exercise.pluck(:sets)
# Array of exercises user has completed in current workout
#completedExercises = CompletedSet.where(:user_id => current_user.id).pluck(:exercise_id)
# Array of exercise_id's in workout times the number of sets for each exercise
#totalExercisesInWorkout = #exercisesInWorkout.zip(#workoutSets).map { |n1, n2| [n1] * n2 }.flatten
#Total number of reps a user has completed
#repsCompleted = CompletedSet.where(:user_id => current_user.id).pluck(:repetitions).sum
#Total amount of weight a user has lifted
#weightLifted = CompletedSet.where(:user_id => current_user.id).pluck(:weight).sum
end
EDIT 2:
I've learned a lot here. I refactored my code. Here is what it looks like now.
Class User
def cycle
Cycle.find(cycle_id)
end
def group
Group.find(group_id)
end
def workout
Workout.find(workout_id)
end
def completed_exercise_ids_array(user)
CompletedSet.where(workout_id: workout_id, user_id: user).pluck(:exercise_id)
end
def sets_in_workout_array
workout.workout_exercises.pluck(:sets)
end
def exercises_in_workout_times_sets_array
workout.workout_exercises.pluck(:exercise_id).zip(sets_in_workout_array).map { |n1, n2| [n1] * n2 }.flatten
end
def update_workout_if_needed!
if exercises_in_workout_times_sets_array.included_in?(completed_exercise_ids_array)
self.workout.id = self.workout.next_workout_id
save!
end
end
# Methods for progress area
def total_reps_completed(user)
CompletedSet.where(user_id: user).sum(:repetitions)
end
def total_weight_lifted(user)
CompletedSet.where(user_id: user).sum(:weight)
end
And my application_controller
class ApplicationController
# Application wide instance variables go here. Just don't forget to call initialize_vars in the controllers you want to call these variables in.
def initialize_vars
#user_workout = current_user.workout
#user_cycle = current_user.cycle
#user_group = current_user.group
#exercise = #user_workout.exercises
# Array of the sets in the users current workout
#workoutSets = current_user.sets_in_workout_array
# Array of exercises user has completed in current workout
#completedExercises = current_user.completed_exercise_ids_array(current_user)
# Array of exercise_id's in workout times the number of sets for each exercise
#totalExercisesInWorkout = current_user.exercises_in_workout_times_sets_array
end
I moved some logic to the profile_controller
An after_create callback for CompletedSet would be the best place for this:
class CompletedSet < ActiveRecord::Base
belongs_to :user
belongs_to :workout
belongs_to :exercise
after_create :update_user_workout
def update_user_workout
user.update_workout_if_needed!
end
end
class User < ActiveRecord::Base
has_many :completed_sets
belongs_to :workout
def completed_exercise_ids
completed_sets.where(workout_id: workout_id).pluck(:exercise_id)
end
def update_workout_if_needed!
if completed_exercise_ids.included_in?(workout.workout_exercises.pluck(:exercise_id))
self.workout = Workout.next(workout)
save!
end
end
end
class Workout < ActiveRecord::Base
has_many :workout_exercises
has_many :exercises, through: :workout_exercises
# some logic to determine the next workout - modify as needed
def self.next(workout)
where("workouts.sequence > ?", workout.sequence).order(:sequence).first
end
end
This solution assumes that you have associated the current_user with the CompletedSet in your controller create action (which I'm not seeing):
def create
#completed_set = current_user.completed_sets.build(params[:completed_set])
...

Passing instance variables into API in rails

I'm trying to pass in some instance variables to call an API with that specific object's attributes. A user fills in their car details (make, model, and year) which creates an offer object. That is supposed to be passed into Edmund's API to retrieve the info for that car. The code works fine if I set it with a specific make/model/year but I can't make it return info for a created offer object.
Here's my controller:
def show
#offer = Offer.find(params[:id])
#wanted_ad = WantedAd.find(params[:wanted_ad_id])
#make = #offer.ownermake
#model = #offer.ownermodel
#year = #offer.owneryear
respond_to do |format|
format.html # show.html.erb
format.json { render json: #offer }
end
end
And here's my model:
class Offer < ActiveRecord::Base
attr_accessible :user_id, :wanted_ad_id, :estvalue, :image1, :offerprice, :ownercartype, :ownerdesc, :ownermake, :ownermileage, :ownermodel, :owneryear
belongs_to :user
belongs_to :wanted_ad
has_one :car
def self.carsearch
#car = []
carinfo = HTTParty.get("http://api.edmunds.com/v1/api/vehicle/#{make}/#{model}/#{year}?api_key=qd4n48eua7r2e59hbdte5xd6&fmt=json")
carinfo["modelYearHolder"].each do |p|
c = Car.new
c.make = p["makeName"]
return carinfo
end
end
end
My car model is simply:
class Car < ActiveRecord::Base
attr_accessible :make, :model, :year
belongs_to :offer
end
And I'm trying to call it from a view file with <%= Offer.carsearch %>. I'm probably all sorts of messed up but this is my first time working with an API and I'm very lost.
I think you got several logical errors in your carsearch method:
You're fetching a carinfo, iterate through an array, instantiate a new car but nothing happens with the c object and at the end of the first iteration you exit the whole function returning the retrieved carinfo...
Is this probably what you've meant?
def carsearch
#cars = []
# where do `make`, `model` and `year` come from here?
# probably method parameters!?
carinfo = HTTParty.get("http://api.edmunds.com/v1/api/vehicle/#{make}/#{model}/#{year}?api_key=qd4n48eua7r2e59hbdte5xd6&fmt=json")
carinfo["modelYearHolder"].each do |p|
c = Car.new
c.make = p["makeName"]
# initialize other attributes (year, model)?
#cars << c
end
return #cars
end

Rails 3: alias_method_chain to set specific attribute first

When user's create a post I'd like to set the user_id attribute first. I'm trying to do this using alias_method_chain on the arrtibutes method. But I'm not sure if this is right as the problem I thought this would fix is still occurring. Is this correct?
Edit:
When my users create a post they assign 'artist(s)' to belong to each post, using a virtual attribute called 'artist_tokens'. I store the relationships in an artist model and a joined table of artist_ids and post_ids called artisanships.
I'd like to to also store the user_id of whomever created the artist that belongs to their post (and I want it inside the artist model itself), so I have a user_id column on the artist model.
The problem is when I create the artist for each post and try to insert the user_id of the post creator, the user_id keeps showing as NULL. Which is highly likely because the post's user_id attribute hasn't been set yet.
I figured to get around this I needed to set the user_id attribute of the post first, then let the rest of the attributes be set as they normally are. This is where I found alias_method_chain.
post.rb
attr_reader :artist_tokens
def artist_tokens=(ids)
ids.gsub!(/CREATE_(.+?)_END/) do
Artist.create!(:name => $1, :user_id => self.user_id).id
end
self.artist_ids = ids.split(",")
end
def attributes_with_user_id_first=(attributes = {})
if attributes.include?(:user_id)
self.user_id = attributes.delete(:user_id)
end
self.attributes_without_user_id_first = attributes
end
alias_method_chain :attributes=, :user_id_first
EDIT:
class ArtistsController < ApplicationController
def index
#artists = Artist.where("name like ?", "%#{params[:q]}%")
results = #artists.map(&:attributes)
results << {:name => "Add: #{params[:q]}", :id => "CREATE_#{params[:q]}_END"}
respond_to do |format|
format.html
format.json { render :json => results }
end
end
In your controller, why not just do this:
def create
#post = Post.new :user_id => params[:post][:user_id]
#post.update_attributes params[:post]
...
end
But it seems to me that it would be much better to create the artist records after you've done validation on the post rather than when you first assign the attribute.
EDIT
I would change this to a callback like this:
class Post < ActiveRecord::Base
attr_accessor :author_tokens
def artist_tokens=(tokens)
#artist_tokens = tokens.split(',')
end
after_save :create_artists
def create_artists
#artist_tokens.each do |token|
...
end
end
end

Resources