Rails adding multiple objects to an empty array
There's a lot going on in your code snippet that is making it not work (or at the very least is unnecessary). You shouldn't be creating a new array in every iteration of your loop. Something along the lines of this should be much better:
@players = @user.players
@teams = Array.new
@players.each do |player|
@teams << Team.find(player.team_id)
end
This will solve your original problem, but it's certainly not the best way of going about what you're trying to do. Add the following to your Player
and User
models:
class Player < ActiveRecord::Base
belongs_to :team
end
class User < ActiveRecord::Base
has_many :players
has_many :teams, through: :players
end
Then, in order to get the teams you're looking for, you can simplify your code to the following:
@teams = @user.teams
You should try going through the documentation not only for ruby arrays, but also the Rails guide for Active Record Associations. Also, in the future try posting the snippet of code first, in order to provide more context to your issue.
Push objects into empty array, return filled array in instance variable. - RoR
If deals
is array you can push it, you should add elements one by one.
@stores.each do |s|
if current_user.voted_for?(s)
s.deals.each { |d| @my_deals << d }
else
end
How do I add multiple elements to an array?
Using +=
operator:
arr = [1]
arr += [2, 3]
arr
# => [1, 2, 3]
How use an array of strings to create multiple ActiveRecord objects at once?
You can create them in one go:
Day.create(days.map { |day| {name: day} })
create
docs mentioned:
Creates an object (or multiple objects) and saves it to the database, if validations pass. The attributes parameter can be either a
Hash
or anArray
ofHashes
.
Pushing objects into an array is returning an empty array
Adding an instance variable queue
to the board class solved this
class Board
def initialize(live_cell_count, queue)
@live_cell_count = live_cell_count
@queue = queue
end
def queue
@queue
end
Inserting queue.push(cell)
in the conditional successfully appends each cell to the queue
array.
def rules(cell)
if something
cell.queue_for_birth
@queue.push(cell)
else something_else
cell.queue_for_death
@queue.push(cell)
end
end
Multiple objects from list of names in an array
Well the error message is pretty clear. You are passing params
as a parameter into batch_kegs_ids
, but that method is expecting no parameters. Besides, it looks like the batch_kegs_ids
method is empty (no code). I'd remove that entirely (probably not supposed to be there anyways).
I'd also change the create_multiple
method to
def create_multiple
params[:batch_keg_ids].each do |id|
BatchKeg.create(keg_id: id, wholesale_inventory: true, taproom_inventory: true, hold_inventory: false, active: true, visible: true)
end
redirect_to batches_url
end
Append a key to all objects in array
You can change your params
params.dig(:data, :events)&.each { |event| event[:session_id] = session.ids }
After that you can call events_params
Don't forget to permit that params
def events_params
params.require(:data).permit(
:session_id,
:user_identifier,
:user_email,
:auth_token,
events: [
:event_type,
:timestamp,
:session_id,
data: {},
],
)
end
Related Topics
Ruby Parenthesis Syntax Exception with I++ ++I
How to Serialize a Ruby Digest::Sha1 Instance Object
Searching from a Range of Ids in Activerecord
Regex for Matching All Words Between a Set of Curly Braces
How to HTML_Escape Text Data in a Sinatra App
Error: Error Installing JSON: Error: Failed to Build Gem Native Extension
Disabling Irb Autocomplete on Heroku
Getting Rails to Accept European Date Format (Dd/Mm/Yyyy)
Interpolating Regexes into Another Regex
Incompatible Character Encoding in Rails - How to Just Fail/Skip Sensibly
Ruby on Rails - £ Sign Troubles
How to Specify Formatting Options for To_Yaml in Ruby
Why Can't We Override '||' and '&&'
Omission of Curly Braces for a Hash in an Array
Regexp Search Through a Very Large File
How to Create a "Clone"-Able Enumerator for External Iteration