Loop through ActiveRecord::Associations::CollectionProxy with each
You probably want to use map
instead:
page.content.map do |item|
item.content_id
end
map
(aka collect
) will iterate through an array and run whatever code you ask it to on the members of the array, one by one. It will return a new array containing the return values for those method calls.
Can't loop through an ActiveRecord_Associations_CollectionProxy object on controller's destroy action?
my understanding you can use callback in your models
open your role model app/models/role.rb
class Role < ApplicationRecord
# callback
before_destroy :check_rules
# here is the callback method that will run automatically before destroy
def check_rules
puts self # in model your @role is equal to self
self.rules.map do |rule|
puts rule
# other action
end
end
end
Rails loop through ActiveRecord::Associations:CollectionProxy
You can do this completely in SQL/ActiveRecord. The query you want to run ultimately is
SELECT article_id, min(date), sum(views)
FROM daily_value_metrics -- Or whatever your table is called
GROUP BY article_id
You can run this with ActiveRecord with the following:
table = DailyValueMetric.arel_table
results = DailyValueMetric.select(table[:article_id],
table[:date].minimum.as('date'),
table[:views].sum.as('views')).group(:article_id).to_a
# Calling to_a so I can call first for the example
results.first.date #=> date
results.first.views #=> views
results.first.article_id #=> Id
The records will look like
[#<DailyViewMetric id: nil, article_id: 1089536>, ...]
Because the SQL query does not return an id column in the result set. This is because of the way that ActiveRecord::Base#inspect
shows the columns defined on the table, and not the returned values outside of the table columns. Views and Date will not necessarily be shown, unless there is a column with the same name, but if you call those attributes on the model instance, you will be able to get the value
Rails 4: Looping through association
According to your code there are no multiple instances of academic
related to the given member
(it's defined using has_one
relation).
The answer is: you cannot loop them.
The correct code for your view should be:
<% if @member.academic.present? %>
<% fields_academic = [:major, :second_major, :minor, :second_minor] %>
<h1>Academics</h1>
<%= render 'database/shared/display', model_obj: @memeber.academic, data: academic, fields: fields_academic %>
<% end %>
<% end %>
If your intention was to have many academics for a memeber, then use has_many
instead.
class Member
has_many :academics
end
Querying an Associations::CollectionProxy
ActiveRecord::Associations::CollectionProxy.find
method uses the same rules as ActiveRecord::Base.find
which is used to retrieve a database row by ID.
So, my_association_collection.find_by(id: 2)
or my_association_collection.find(2)
will always execute a SELECT
query on the database, not reference the existing result.
See doc
Rails method on ActiveRecord::Associations::CollectionProxy
I found out how. Using a self
method then all
:
def self.format
all.each do |item|
# Manipulate items here
end
end
However, I ended up having a method like this:
def format
{
id: id,
note: 'Contact Note',
# Etc
}
end
Then just used:
customer.notes.map {|i| i.format }
RSpec has_many through #ActiveRecord::Associations::CollectionProxy on after_save
The problem is that the aliases are not yet persisted and pluck
does a database query (see when you inspect address_aliases
, the record does not have an ID yet, it's on memory, it's not on the database yet).
Replace that pluck
with map(&:alias_for_city)
so it doesn't do a database query and uses the already loaded collection.
Related Topics
Ruby on Rails: Provide VS Content_For
No Implicit Conversion of String into Integer (Typeerror)
Conditional Tag Wrapping in Rails/Erb
Adding a Method to Built-In Class in Rails App
Confusing Behaviour of Const_Get in Ruby
How Are Symbols Used to Identify Arguments in Ruby Methods
How to Uninstall Ruby on Rails and Do a Clean Install
MAC Os X Mountain Lion "Rails Is Not Currently Installed on This System."
How to Use Rails and Paperclip to Store Photos on Google Cloud Storage
Define a Method That Is a Closure in Ruby
Ruby/Rails Using || to Determine Value, Using an Empty String Instead of a Nil Value
How to Return Multiple Tags from a Rails Helper
How to Do a Regex Search in Nokogiri for Text That Matches a Certain Beginning