Best way to count ActiveRecord child custom properties? - rails-activerecord

If I have a shopping cart, and in it is produce (where each produce has a custom expired property), what is the best way to count the expired property? -- is there a better way than this count_expired?
class Cart < ActiveRecord::Base
has_many :produce, :dependent => :delete_all
def count_expired
count = 0
self.produces.each do | produce |
if produce.expired
count ++
end
end
return count
end
end
class Produce < ActiveRecord::Base
belongs_to :cart, class_name: "Cart", touch: true
def expired
return <true or false logic>
end
end

Related

Access childrens using where condition for nested attributes

I have the following relationship set, dashboard filter values have a column called filter_type which can have value 1 or 0.
class DashboardFilterValue < ApplicationRecord
belongs_to :dashboard_filter
end
class DashboardFilter < ApplicationRecord
has_many :dashboard_filter_values, dependent: :destroy
accepts_nested_attributes_for :dashboard_filter_values
before_save :check_parameter_length
def check_parameter_length
Rails.logger.info self.dashboard_filter_values.inspect #prints the ActiveRecord::Associations::CollectionProxy
Rails.logger.info self.dashboard_filter_values.where(:filter_type => 0) #does not print anything
end
end
In the before_save callback,
When I use self.dashboard_filter_values.inspect, this prints
ActiveRecord::Associations::CollectionProxy.
But self.dashboard_filter_values.where(:filter_type => 0) does not print anything, even when there are records which satisfy the condition.
In the before_save callback, how can I use the where condition to filter values that I want.
Any help in this would be really great. Thanks.
I believe this is not working because of the before_save action. When you use where it is performing a database query, but because you are querying the database before it saves, nothing is returned.
I would say you have 2 options:
Convert it to an after_save
Use Enumerable#select instead:
Rails.logger.info self.dashboard_filter_values.select { |filter| filter.filter_type == 1 }

Update on has_many should update attribute on the parent class

I have a User model which has_many :scores.
If I add a :score to the user, the user should do a recalculation of the playcounter:
class User < ActiveRecord::Base
has_many :scores, inverse_of: :user
accepts_nested_attributes_for :scores
attr_accessible :level
before_save: set_levels
def set_levels
self.level = calculate_level
end
def calculate_level
self.scores.count
end
end
When I add a score via User.score.create(:time => 10) the score get's saved but the level does not get updated.
How can I rerun the set_levels if a child was attached?
(it's not a countercache column, method it's more complex, just sketched here)
many thanks
You need to add a callback to the Score model, perhaps an after_commit on the score model to tell the parent User to recalculate the level.
class Score < ActiveRecord::Base
after_commit do
# can just call touch, as before_save will calc
user.touch
end
end
Now, that is only if you want to recalculate on every score, and synchronously.
More often I will drop a message, such as to resque/sidekiq, to do this, so my score create/updates are fast.
class Score < ActiveRecord::Base
after_commit do
UpdateUserLevelWorker.perform_async(user_id)
end
end
class UpdateUserLevelWorker
include Sidekiq::Worker
def perform(user_id)
# can just call touch, as before_save will calc
User.find(user_id).touch
end
end
I've got it working with
after_create :set_level
def set_level
self.update_attribute :level, calculate_level
end

How to have an ordered model association allowing duplicates

I have two models, Song and Show. A Show is an ordered list of Songs, in which the same Song can be listed multiple times.
That is, there should be an ordered array (or hash or anything) somewhere in Show that can contain Song1, Song2, Song1, Song3 and allow re-ordering, inserting, or deleting from that array.
I cannot figure out how to model this with ActiveRecord associations. I'm guessing I need some sort of special join table with a column for the index, but apart from starting to code my SQL directly, is there a way to do this with Rails associations?
Some code as I have it now (but doesn't work properly):
class Song < ActiveRecord::Base
attr_accessible :title
has_and_belongs_to_many :shows
end
class Show < ActiveRecord::Base
attr_accessible :date
has_and_belongs_to_many :songs
end
song1 = Song.create(title: 'Foo')
song2 = Song.create(title: 'Bar')
show1 = Show.create(date: 'Tomorrow')
show1.songs << song1 << song2 << song1
puts "show1 size = #{show1.songs.size}" # 3
show1.delete_at(0) # Should delete the first instance of song1, but leave the second instance
puts "show1 size = #{show1.songs.size}" # 2
show1.reload
puts "show1 size = #{show1.songs.size}" # 3 again, annoyingly
Inserting might look like:
show1.songs # Foo, Bar, Foo
song3 = Song.create(title: 'Baz')
show1.insert(1, song3)
show1.songs # Foo, Baz, Bar, Foo
And reordering might (with a little magic) look something like:
show1.songs # Foo, Bar, Foo
show1.move_song_from(0, to: 1)
show1.songs # Bar, Foo, Foo
You're on the right track with the join table idea:
class Song < ActiveRecord::Base
attr_accessible :title
has_many :playlist_items
has_many :shows, :through => :playlist_items
end
class PlaylistItem < ActiveRecord::Base
belongs_to :shows #foreign_key show_id
belongs_to :songs #foreign_key song_id
end
class Show < ActiveRecord::Base
attr_accessible :date
has_many :playlist_items
has_many :songs, :through => :playlist_items
end
Then you can do stuff like user.playlist_items.create :song => Song.last
My current solution to this is a combination of has_many :through and acts_as_list. It was not the easiest thing to find information on combining the two correctly. One of the hurdles, for example, was that acts_as_list uses an index starting at 1, while the array-like methods created by the ActiveRecord association start at 0.
Here's how my code ended up. Note that I had to specify explicit methods to modify the join table (for most of them anyway); I'm not sure if there's a cleaner way to make those work.
class Song < ActiveRecord::Base
attr_accessible :title
has_many :playlist_items, :order => :position
has_many :shows, :through => :playlist_items
end
class PlaylistItem < ActiveRecord::Base
attr_accessible :position, :show_id, :song_id
belongs_to :shows
belongs_to :songs
acts_as_list :scope => :show
end
class Show < ActiveRecord::Base
attr_accessible :date
has_many :playlist_items, :order => :position
has_many :songs, :through => :playlist_items, :order => :position
def song_at(index)
self.songs.find_by_id(self.playlist_items[index].song_id)
end
def move_song(index, options={})
raise "A :to option is required." unless options.has_key? :to
self.playlist_items[index].insert_at(options[:to] + 1) # Compensate for acts_as_list starting at 1
end
def add_song(location)
self.songs << location
end
def remove_song_at(index)
self.playlist_items.delete(self.playlist_items[index])
end
end
I added a 'position' column to my 'playlist_items' table, as per the instructions that came with acts_as_list. It's worth noting that I had to dig into the API for acts_as_list to find the insert_at method.

Rails callback after_save not setting attribute

I'm dealing with a problem on a after_save callback. I'm sure there is a easy solution, but I can't figure it out.
I have 3 models: User, Product, Bid. The Product table contains a boolean field "available", which is set default to true. If a User places a bid, the available field should be set to false.
I thought this should work with a callback on the bid model.
I can access and set the available field in the console by typing:
b = Bid.last
b.product.available = false
=> false
However I can't change it via the controller, so I think it doesn't execute the callback. What am I doing wrong?
Thank you all for your help!
product.rb
class Product < ActiveRecord::Base
has_one :bid
belongs_to :user
end
bid.rb
class Bid < ActiveRecord::Base
attr_accessible :product_id, :user_id, :product
belongs_to :product
belongs_to :user
after_save :set_product_status
def set_product_status
self.product.available = false
end
end
bids_controller.rb
...
def create
#user = current_user
product = Product.find(params[:product_id])
#bid = #user.bids.build(product: product)
respond_to do |format|
if #bid.save
...
Since bid belongs_to product, you should save the product too.
def set_product_status
self.product.available = false
self.product.save
end

Rails 3. Decide on save if the object should be saved or not

iam just asking myself, whats the best solution for my problem.
Here are my models:
class Product < ActiveRecord::Base
has_many :prices, :class_name => "ProductPrice"
accepts_nested_attributes_for :prices
end
class ProductPrice < ActiveRecord::Base
belongs_to :product
end
The controller
def create
#product = Product.new(params[:product])
#product.save
...
end
What i want to do is to prevent all ProductPrices from being saved when product_price.value == nil or product_price.value == 0.0
before_save hook in ProductPrice. return false will rollback the whole transaction, thats not what i want to do. i just want to "kick" all prices with value == 0 or value == nil
first kick all price_params from params[...] and than call Product.new(params[:product]) seems not to be the rails way eighter...
after Product.new(params[:product]) iterate over all prices and delete them from the array. but the logic should be in my models right? i just dont want to repeat myself on every controller that creates new prices...
can someone tell me the best solution for that? whats the rails way?
thanks!
What you want it called a validation hook, something like this:
class ProductPrice < ActiveRecord::Base
belongs_to :product
validates :value, :numericality => {:greater_than => 0.0 }
end
See http://guides.rubyonrails.org/active_record_validations_callbacks.html for other ways you may want to do this with finer control.
To avoid adding these invalid prices in the first place, you can remove them from the nested attributes hash like this:
class Product < ActiveRecord::Base
def self.clean_attributes!(product_params)
product_prices = product_params['prices'] || []
product_prices.reject!{|price| price['value'].to_f == 0 rescue true }
end
end
Product.clean_attributes!(params[:product])
Product.new(params[:product])

Resources