I'm not sure what I'm doing wrong here but I want to create scope for a model I have, but I want it to evaluated a count on a related model... like say:
class Thing < ActiveRecord::Base
has_many :photos
scope :with_images, self.photo.count > 0
end
class Photo < ActiveRecord::Base
belongs_to :thing
end
I should then have a scope which would work like
Thing.where('some conditions').with_images
I get a NoMethodError on photos, why wouldn't this be available as a relation? I don't want to use it as a method.
There are two things going on here. First, you're trying to call photo, instead of photos.
However, the you're still going to get an error because at the time of execution, self refers to the constant Thing, and not an instance of Thing. The declaration has_many :photos defines a method photos for instances of Thing. Therefore, Thing (the constant) doesn't have a method called photos.
tl;dr Just use a :joins argument since it will only find records that have photos
scope :with_images, :joins => :photos
It should be:
self.photos.count > 0
or if you're using counter cache:
self.photos_count > 0
Related
I would like to use the includes method with the related element of my Post
My Post can be associated with different type of element. And I use a value :cat to knows witch kind of element is associated.
The value work as this (cat: (1 => Message, 2=>Question, 3=>Task, 4=>Event) with the association has_one
Example : If post.cat == 3, I can call the task related with a method post.task
Now, I would like to optimize the SQL requests of my Post/Index with the method includes. But is not working for the moment. Can you help me to find the error of my code ?
Post_controller :
def index
#posts = current_user.posts
#posts.each do |post|
if post.cat == 3
#task = post.task.includes(:users)
elsif post.cat == 4
#event = post.event.includes(:reminds)
end
end
end
Error: undefined method `includes'
Edit :
Post_model:
class Post < ApplicationRecord
has_one :post_message, dependent: :destroy
has_one :question, dependent: :destroy
has_one :task, dependent: :destroy
has_one :event, dependent: :destroy
end
Task_model :
class Task < ApplicationRecord
belongs_to :post
has_many :users_task, dependent: :destroy
has_many :users, through: :users_task
end
Why are you using #posts.each ?
For me, the best solution for that is to find all the posts whith the defined cat to run the includes method. In your case, it would be like that :
#posts.where(cat: 1).includes(:message)
#posts.where(cat: 2).includes(:question)
#posts.where(cat: 3).includes(task: :users)
#posts.where(cat: 4).includes(event: :reminds)
Well, after many tries, I opted for a scope method to run the includes method. It's not a really elegant solution, but I think it's the best in my case.
So I'm preparing the scopes in my Post_Model:
scope :with_tasks, -> { where(cat: 3).includes(:user).includes(task: :users) }
scope :with_events, -> { where(cat: 4).includes(:user).includes(event: :reminds) }
And after, I render them in my index action like this :
#posts = current_user.posts.with_tasks + current_user.posts.with_events
So the code is generating 2 SQL Requests to find the posts (one for each category).
I think there is a way to join all that directly into a new global scope, but I don't know how. So if there is anyone knows that, he can edit the answer
Enjoy !
If you're getting an undefined method: 'includes' error, it means that either post.task or post.event are not returning ActiveRecord objects like your code is expecting. Are you sure there will always be values set for .task or .event at that point in execution? Are there any cases where that value might be nil or blank?
By the way, have you heard about 'polymorphic associations'? Defining an association as polymorphic allows you to associate records of arbitrary types with a specific column (by storing both object ID and class name on each record behind the scenes). It seems like this exactly matches your use case. It would be much easier to use the built-in mechanism than trying to do all the if-then switching based on category in your code.
I'm trying to scope the main group of my user. This group is noted with a cat: which is 2.
So I thought of doing this with a scope like
class User < ApplicationRecord
has_many :users_group, dependent: :destroy
has_many :groups, through: :users_group
scope :my_group, -> { self.joins(:groups).where('groups.cat = 2').limit(1) }
end
But the command below is not working :
current_user.my_group
Can you lead me on the good way to achieve it ?
As Mario says, a scope works on a collection, not an instance.
If you want to keep the method in the User model you can use the following:
user.rb
def my_group
groups.find_by_cat(2)
end
Using find_by will return a single group, rather than using where / limit. If the group isn't found, it will return nil.
I'd suggest using a scope to return a single instance is a bit of an anti-pattern, and it would be better achieved using this method, or dropping the following method into Group and calling current_user.groups.my_group - although the name my_group sounds a bit out of place like that. For completeness, here it is regardless:
group.rb
def my_group
find_by_cat(2)
end
current_user doesn't return an ActiveRecord relation, it just returns the user so you can't chain it together with a scope (I'm assuming the error message you're getting is undefined method 'my_group' for #<User>?). Add the scope to your Group class and use it through your groups has_many relationship e.g.
current_user.groups.my_group
i am really struggling with SQL search queries today. Can one kindly tell me how i write a scope that display events with no payments.
event.rb
has_many :payments
payment.rb
belongs_to :event
i tried writing the below scope in the terminal:
events.joins(:payments).where("event.payments.empty?")
i also tried:
events.where("payments.empty?")
i am very unsure how to write a scope that displays an object with an empty array
This should work
Event.includes(:payments).where(payments: { event_id: nil })
class User < ApplicationRecord
has_many :buckets, -> { order(ranked_row: :asc) }
delegate :incomes, :fixed_costs, :financial_goals, to: :buckets
...
end
I have Buckets, that are STI'd. If I add that scope to the has_many, my page takes forever on 9 records, and seems to be loading something that should be cached
If I remove the scope, all is well
Any idea how the scope on the has_many is affecting the STI?? ranked_row has an index, but it's the same, regardless. I am using active_model_serializers, but I'm not sure if there's a correlation.
Update
Definitely something with active_model_serializers. ActiveModel::SerializableResource.new(user) is in the controller, and bogs down in the console, too. I removed everything from the from the serializer, and calling the scoped has_many is the thing. I'll hit up github.
Code
https://gist.github.com/dudo/f25767f00c874842a005
That's the smallest bit of code I could get to cause the issue. Again, It works fine without the scope on the has_many, and it also works with removing the percent_complete method from Bucket... that method doesn't look too nasty. What could be in that included_transactions method that's bringing it to a crawl when a scope is present??
When adding scope to a has_many you need to explicitly declare the inverse_of.
https://github.com/rails/rails/blob/7f18ea14c893cb5c9f04d4fda9661126758332b5/guides/source/4_1_release_notes.md
The Setup
I have an STI setup like so:
class Transaction < ActiveRecord::Base
belongs_to :account
scope :deposits, -> { where type: Deposit }
end
class Deposit < Transaction
scope :pending, -> { where state: :pending }
end
class Account < ActiveRecord::Base
has_many :transactions
end
If I call:
> a = Account.first
> a.transactions.deposits
...then I get what I expect, a collection of Deposit instances, however if I look at the class of what's returned:
> a.transactions.deposits.class
...then it's actually not a Deposit collection, it's still a Transaction collection, ie. it's a Transaction::ActiveRecord_AssociationRelation
The Problem
So, to the problem, if I then want to call one of the Deposit scopes on that collection it fails:
> a.transactions.deposits.pending
NoMethodError: undefined method `pending' for #<Transaction::ActiveRecord_Associations_CollectionProxy:0x007f8ac1252d00>
Things I've Checked
I've tried changing the scope to Deposit.where... which had no effect, and also to Deposit.unscoped.where... which actually returns the right collection object, but it strips all the scope, so I lose the account_id=123 part of the query so it fails on that side.
I've checked this and the problem exists for both Rails 4.1 and 4.2. Thanks for any pointers on how to make this work.
I know there's a workaround, but...
I know I could work around the issue by adding a has_many :deposits into Account, but I'm trying to avoid that (in reality I have many associated tables and many different transaction subclasses, and I'm trying to avoid adding the dozens of extra associations that would require).
Question
How can I get what's returned by the deposits scope to actually be a Deposit::ActiveRecord_Association... so that I can chain my scopes from Deposit class?
I created an isolated test for your issue here:https://gist.github.com/aalvarado/4ce836699d0ffb8b3782#file-sti_scope-rb and it has the error you mentioned.
I came across this post from pivotal http://pivotallabs.com/merging-scopes-with-sti-models/ about using were_values in a scope to get all the conditions. I then used them on unscope to force the expected class, basically this:
def self.deposits
conditions = where(nil).where_values.reduce(&:and)
Deposit.unscoped.where(conditions)
end
This test asserts that it returns a Deposit::ActiveRecord_Relation https://gist.github.com/aalvarado/4ce836699d0ffb8b3782#file-sti_scope2-rb
Update
You can also write this as a scope if you prefer:
scope :deposits, -> { Deposit.unscoped.where where(nil).where_values.reduce &:and }
As a quick workaround you can do > a.transactions.deposits.merge(Deposit.pending), but can't think of a different way of solving it. I'll think and try more options later and come back if I find anything.
You might want to say that an Account has_many :deposits
class Account < ActiveRecord::Base
has_many :transactions
has_many :deposits
end
Then you should be able to query
a.deposits.pending