Using a scope to access a unique single record without having to loop? - ruby-on-rails-3

I have a model Enrollment which validates the uniqueness for each course by user:
validates_uniqueness_of :user_id, :scope => :course_id
I created a scope in the Enrollment model where I can pass in the course to narrow down the course by user:
scope :course, lambda { |course| where(:course_id => course.id) }
By doing this, I can call:
current_user.enrollments.course(#course)
However, it then makes me loop through the result, even though there is only one result (unique course for each user). This is fine, but it seems like there should be a way to simply do the scope and then just access the record without need to loop through one result.
Any ideas? I feel like I'm missing something.

Try this:
current_user.enrollments.course(#course).first
That will generate SQL query with LIMIT 1 statement and return the model directly without wrapping it into an array.
Small note about your scope. As your Enrollment model has course association (has_one :course I assume) you'd better give your scope different name like by_course to prevent collision with course association that allows you to fetch course for a given enrollment.

Related

rails sql find models related to self with attribute difference greater than threshold

I have a Person model in rails that is related to itself:
class Person < ApplicationRecord
has_many :children,
class_name: "Person",
foreign_key: :person_id
belongs_to :mother,
class_name: "Person",
foreign_key: :person_id
end
People also have an age attribute representing their ages in years. I would like an ActiveRecord or SQL query to find all people with child-to-mother age discrepancies that are greater than or equal to 50 years.
(The model name here is not what I'm truly using in my app. I renamed it to simplify the problem description. However, for my actual query I will need to know if the absolute value of the age difference is greater than or equal to 50 years. Obviously children can't be older than their mothers so the analogy doesn't hold there.)
I have tried the following and several variants, which are all crashing:
Person
.includes(:mother)
.where("ABS(persons.age - (SELECT age FROM persons WHERE (id = persons.person_id))) >= 50")
This one in particular results with a hint:
HINT: No operator matches the given name and argument types. You might need to add explicit type casts.
At first I thought that maybe the latest SQL querie's where clause was returning some sort of list of ages rather than just the mother's age, so I tried subscripting with a [1] after it to get the first element of (what should be) a single element array (if my hypothesis was correct). It was not correct - the SQL error then updated to let me know I couldn't subscript something that wasn't an array.
I'm looking for the simplest solution. Any help is appreciated. Thanks!
Not in a position to test this but I believe reails will generate aliases for you on the fly.
Try
Person
.includes(:mother)
.where("ABS(persons.age - (SELECT age FROM mothers_persons)) >= 50")
Generally it's association name pluralized, underscore, table name (so if users have employees the associated table (alias) of user's employees is called employees_users
If the above doesn't work, you would need to create aliases manually in your SQL call.

Rails/ActiveRecord: Can I perform this query without passing the SQL string to #order?

I have two models Issue and Label. They have a many to many relationship.
I have a method that returns the ten labels that point to the most issues.
class Label < ApplicationRecord
has_many :tags
has_many :issues, through: :tags
def self.top
Label.joins(:issues)
.group(:name)
.order('count_id desc')
.count(:id)
.take(10)
end
end
It does exactly what I expect it to but I want to know if it's possible to compose the query without the SQL string.
order('count_id DESC') is confusing me. Where does count_id come from? There isn’t a column named count_id.
Label.joins(:issues).group(:name).column_names
#=> ["id", "name", "created_at", "updated_at"]
I’ve found some SQL examples here. I think it’s basically the same as ORDER BY COUNT(Id):
SELECT COUNT(Id), Country
FROM Customer
GROUP BY Country
ORDER BY COUNT(Id) DESC
Is it possible to perform the same query without passing in the SQL string? Can it be done with the ActiveRecord querying interface alone?
If you look at your query log, you'll see something like:
select count(labels.id) as count_id ...
The combination of your group call (with any argument) and the count(:id) call gets ActiveRecord to add the count_id column alias to the query. I don't think this is documented or specified anywhere (at least that I can find) but you can see it happen if you're brave enough to walk through the Active Record source.
In general, if you add a GROUP BY and then count(:x), Active Record will add a count_x alias. There's no column for this so you can't say order(:count_id), order(count_id: :desc), or any of the other common non-String alternatives. AFAIK, you have to use a string but you can wrap it in an Arel.sql to prevent future deprecation issues:
Label.joins(:issues)
.group(:name)
.order(Arel.sql('count_id desc'))
.count(:id)
.take(10)
There's no guarantee about this so if you use it, you should include something in your test suite to catch any problems if the behavior changes in the future.

Chaining scopes with joins

these two scopes don't seem to be chainable
scope :approved, ->{ with_stage(:approved)}
which in sql is
WHERE (pages.stage & 4 <> 0)
and
scope :with_galleries, ->{ joins("LEFT OUTER JOIN galleries ON galleries.galleriable_type = 'Brand' AND galleries.galleriable_id = page.brand_id").where("galleries.id is NOT NULL") }
this scope should give only the pages that have galleries (each page has one brand and each brand can have many galleries)
if I chain the :with_galleries, it seems that the rest of the conditions on pages table is lost
Am I doing the joins wrong?
You would get a more useful result if you let ActiveRecord do more of the heavy lifting for you. In particular, if you've set up associations properly, you should be able to write the following instead:
scope :with_galleries, joins(brand: :galleries)
... which would yield a properly chainable scope.
That would depend on two associations, one from your page model to the brand:
'belongs_to :brand'
and one from the brand to the galleries::
has_many :galleries, as: :galleriable
I'm inferring your model setup from the query that you've written, so I may have guessed wrong. But the basic principle here is to declare your associations and let ActiveRecord construct queries (unless your query is something very unusual, which yours is not -- you're just filtering depending on whether there are associated records, a common operation).
You need to construct the second scope using Arel. The simplest approach is to craft the full SQL statement you want the second scope to represent, and then paste it into http://www.scuttle.io/

Avoiding db hits in ActiveRecord

When I assign a database find to an instance variable in Rails, why do future requests to that variable also hit the database? Can this be avoided?
For example, I have 3 models: User, Resource, Opinion, with has_many :through on Opinion
#opinions = current_user.opinions # pulls in all of the user's opinions, which include respective resource ids
1. Calling for resource_id directly does not hit the database:
#opinions.each do |opinion|
opinion.resource_id # does not hit the database (as expected)
end
2. Performing a query does hit the database (even though variable has been assigned):
#opinions.find_by_resource_id(1) # DOES hit the database
Why does #2 hit the database? Is there a way to perform the same find without hitting the database?
The information is already contained in the #opinions variable, so a db call does not seem necessary.
If you don't need anything else in the #opinions array, I would scope your original query to only include opinions with that resource_id
#opinions = current_user.opinions.where("resource_id = ?", resource_id)
If you already have #opinions and just want to create a new array of objects that match for a specific key/value:
#opinions_with_resource_id = #opinions.select { |opinion| opinion.resource_id == 1234 }
Check out this other answer for another explanation or if you want to split the answer into multiple arrays.
Thoughts
Comment on your last piece of code
Methods like you called find_by_* are dynamic finders that use method_missing to hit the database and look inside of the column specified by the *.
Remaining comments from previous answer
If this object will ever need to access data on the Resource model, don't forget about the #includes() method, which will keep you from having to run additional queries down the road.
#opinions = current_user.opinions.includes(:resources)
See http://guides.rubyonrails.org/active_record_querying.html#eager-loading-associations

Allow users to create dynamic model attributes?

In my Rails3 app, I am using ActiveRecord and Postgresql.
Say I have a model called Parts. The model has a small list of standard attributes such as price, quantity, etc.
However, Customer A might want to add LotNumber and CustomerB might want to add OriginalLocation.
How would I allow them to do that?
I thought about creating a PartsDetail model that allowed them to have a type.
class PartsDetail < ActiveRecord::Base
attr_accessible :type, :value, :part_id
belongs_to :parts
end
So that "type" could be "LotNumber", etc.
But I'm not quite sure how that would work in my associations and querying.
Any ideas?
Thanks.
Since you're using PostgreSQL, you could use hstore to store arbitrary hashes in database columns:
This module implements the hstore data type for storing sets of key/value pairs within a single PostgreSQL value. This can be useful in various scenarios, such as rows with many attributes that are rarely examined, or semi-structured data. Keys and values are simply text strings.
There's even a gem for adding hstore support to ActiveRecord:
https://github.com/softa/activerecord-postgres-hstore
Then you could create an hstore column called, say, client_specific and look inside it with things like:
M.where("client_specific -> 'likes' = 'pancakes'")
M.where("client_specific #> 'likes=>pancakes'")
to see which clients have noted that they like pancakes.
You might want to store a list of customer-specific fields somewhere with the customer record to make the UI side of things easier to deal with but that's pretty simple to do.
Here's a gist that someone wrote that allows you to use hstore attributes in your models more seamlessly: https://gist.github.com/2834785
To use add this in an initializer ( or create a new initializer called active_record_extensions.rb )
require "hstore_accessor"
Then in your model you can have:
Class User < ActiveRecord::Base
hstore_accessor :properties, :first_name, :last_name
end
That allows you to do:
u = User.new
u.first_name = 'frank'
You can still do add attributes to the hstore column and bypass the hstore_attributes though:
u.properties['middle_name'] = 'danger'