This is my scenario:
class User < ActiveRecord::Base
has_many :things
# attr_accessible :average_rating
end
class Thing < ActiveRecord::Base
belongs_to :user
has_one :thing_rating
end
class ThingRating < ActiveRecord::Base
belongs_to :thing
attr_accessible :rating
end
I want to have an attribute in my User model which has the average calculation of his related ThingsRating.
What would be the best practice to manage this?
Thanks
May be you can use relation not sure but you can try this
class User < ActiveRecord::Base
has_many :things
has_many :thing_ratings, through: :things
# attr_accessible :average_rating
def avg_rating
#avg_rating ||= thing_ratings.average("thing_ratings.rating")
end
end
The easy way :
class User < ActiveRecord::Base
has_many :things
def avg_rating
#avg_rating ||= average(things.map(&:thing_rating))
end
private
def average(values)
values.inject(0.0) { |sum, el| sum + el } / arr.size
end
end
This is fine as a starter. But if you have a bit of trafic, you might find yourself with scaling problems.
You'll then have to refactor this to avoid making an SQL query to the things every time you call the method for a different user.
You could then have several possibilites :
Add a field in your User database, avg_rating, which would be updated by the ThingRating when it's created or updated.
Use a memcached or redis database to cache the value and invalidate the cache every time a ThingRating is updated or created.
These solutions aren't exhaustive of course. And you could find other ones which would better fit your needs.
Related
I am building a simple budgeting app, and have a line of code that feels convoluted and overly complex. For context:
class User < ActiveRecord::Base
has_one :month_budget
has_many :expenditures, as: :spendable
end
class MonthBudget < ActiveRecord::Base
belongs_to :user
has_many :expenditures, as: spendable
end
class Expenditure < ActiveRecord::Base
belongs_to :spendable, polymorphic: true
end
Within my Expenditure class, I have defined a class method, add_expenditure:
class Expenditure < ActiveRecord::Base
def self.add_expenditure(user, params) #params passed will be in [:expenditure][*keys], in which possible keys are [:amount] or [:location]
if user.month_budget
user.month_budget.expenditures.create(params)
new_amount = user.month_budget.current_amount += params[:amount].to_d
user.month_budget.update(current_amount: new_amount)
end
end
end
Is there a more efficient way to add a value to the initial month_budget.current_amount column, and then record this new number to the database?
Cheers in advance!
Maybe you could try increment! method (http://apidock.com/rails/v4.2.1/ActiveRecord/Persistence/increment%21).
However, I am not sure if it works well with big decimals.
class Expenditure < ActiveRecord::Base
def self.add_expenditure(user, params)
if user.month_budget
user.month_budget.expenditures.create(params)
user.month_budget.increment!(:current_amount, params[:amount].to_d)
end
end
end
I have 3 relevant models in a Rails 4 app - Charge:
class Charge < ActiveRecord::Base
belongs_to :rate
belongs_to :shift
def total
self.rate.value * self.quantity
end
end
Rate:
class Rate < ActiveRecord::Base
has_many :charges
end
and Shift:
class Shift < ActiveRecord::Base
has_many :charges
def total_charge
self.charges.sum('total')
end
end
I'm attempting to use shift.total_charge in my view, but I'm getting the error:
SQLite3::SQLException: no such column: total: SELECT SUM(total) FROM "charges" WHERE "charges"."shift_id" = ?
So it seems that it isn't possible to define total in the Charge model in this way, and have it accessible to sum from the Shift model as an actual column would be. I'm struggling to find the appropriate Rails 4 way of doing this - is it possible to do this in the model, or do I need to create a controller for Charge and try to do the calculation there?
sum works only with columns. You could use something like
class Shift < ActiveRecord::Base
has_many :charges
def total_charge
self.charges.map {|c| c.rate.value * c.quantity }.sum
end
end
and to avoid n+1 problem include Rate in Charge
class Charge < ActiveRecord::Base
belongs_to :rate
belongs_to :shift
default_scope {includes :rate}
end
Example:
I have the following:
class Person < ActiveRecord::Base
has_many :educations
end
class Education < ActiveRecord::Base
belongs_to :school
belongs_to :degree
belongs_to :major
end
class School < ActiveRecord::Base
has_many :educations
# has a :name
end
I want to be able to return all people who went to a specific school so in my PeopleController#index I have
#search = Person.search do
keywords params[:query]
end
#people = #search.results
How do I create the searchable method on the Person model to reach down into school? Do I do something like this:
searchable do
text :school_names do
educations.map { |e| e.school.name }
end
end
which I would eventually have to do with each attribute on education (degree etc) or can I make a searchable method on Education and somehow "call" that from Person.searchable?
Thanks
It would be best if you keep the declaration of all the indexed fields for an specific model in the same place.
Also, you were doing a good job indexing :school_names, just do the same thing for the rest of the associations fields' that you want to index.
Here is my scenario:
A model called Course has many CourseCodes. A CourseCode belongs to a Course.
A CourseCode can't be created without Course and a Course can't be created without at least one CourseCode.
class Course < ActiveRecord::Base
has_many :course_codes
validate :existence_of_code
private
def existence_of_code
unless course_codes.any?
errors[:course_codes] << "missing course code"
end
end
end
class CourseCode < ActiveRecord::Base
belongs_to :course
validates_presence_of :course
end
The whole scenario feels a bit like catch 22.
Is there a way to create both on the same time?
I'm using Rails 3.2
Solved the problem by using accepts_nested_attributes_for.
Nested attributes allow you to save attributes on associated records through the parent. By default nested.
class Course < ActiveRecord::Base
has_many :course_codes, inverse_of: :course
validate :existence_of_code
accepts_nested_attributes_for :course_codes
private
def existence_of_code
unless course_codes.any?
errors[:course_codes] << "missing course code"
end
end
end
class CourseCode < ActiveRecord::Base
belongs_to :course, inverse_of: :course_codes
validates_presence_of :course
end
Used like this.
Course.create!({
course_codes_attributes: [{ code: "TDA123" }],
# ...
})
Looks good to me. Removing the validates_presence_of :course might make things easier on you, too, as it will tend to get in the way an not add much.
When you create a course, do it like this:
Course.create course_codes: [CourseCode.new(...), CourseCode.new(...)]
ActiveRecord will figure things out.
You could add an unless to whichever model you would plan to create first. For instance:
class CourseCode < ActiveRecord::Base
belongs_to :course
validates_presence_of :course, :unless => lambda { Course.all.empty? }
end
I have two ActiveRecord models with a hasMany / belongsTo association:
class User < ActiveRecord::Base
has_many :letters
end
class Letter < ActiveRecord::Base
belongs_to :user
end
The User model has a revision_number attribute, to which I would like to scope the belongs_to association, so the letter is associated to a User by both user.id and user.revision_number.
I tried using the :conditions key as documented in the API docs:
class Letter < ActiveRecord::Base
belongs_to :user, :conditions => "revision_number = #{client_revision}"
end
but this attempts to call client-revision on the Letter class, not the instance of Letter. Could anyone point me in the right direction for scoping the belongs_to association correctly?
I'm using the acts-as-revisable plugin to version the User model.
I am having a hard time understanding why you would want to scope the belongs_to in this way. Correct me if I am wrong, but it might be better to do something like this. I am assuming you want some sort of version control system:
class User < ActiveRecord::Base
has_many :letters
end
class Letter < ActiveRecord::Base
has_many :revisions, :class_name => "LetterVersion"
belongs_to :current, :class_name => "LetterVersion"
end
class LetterVersion < ActiveRecord::Base
belongs_to :letter
end
Finally figured out what I needed was something like composite keys, which Rails ActiveRecord doesn't support. The solution (for now at least) was to write custom client accessors on the Letter to support the composite keys (id and revision_number):
class Letter < ActiveRecord::Base
def client
Client.find_by_id(self.client_id).try(:find_revision, self.client_revision)
end
def client=(c)
self.client_id = c.id
self.client_revision = c.revision_number
end
end
class Client < ActiveRecord::Base
acts_as_revisable
has_many :letters
end
With this setup, Client#1.letters will retrieve an array of both letters, but Letter#2.client will retrieve Client#1r2, whilst Letter#2.client will retrieve Client#1r4:
Client id: 1 1 1 1 1 1
rev_number: 1 2 3 4 5 6
Letter id: 1 2
client_id: 1 1
client_revision: 2 5
Still not sure if this is the best approach to this problem, but it seems to work for now.