I am doing the below to get all photos that are tagged with the same tag. What I also want to get is a list of the Albums in which those photos appear.
#photos = Photo.tagged_with(#tag)
I've tried doing the following but it fails.
#albums = #photos.albums
Any ideas?
#photos is not a singular object, it will return an array of ActiveRecord objects, so you can't do #photos.albums. Assuming there's a one-to-many relationship between the two models, then you have to get each photo then get the albums associated with it.
Related
I have the model Post, that via polymorphism belongs to either Organization, Team or User.
For each parent I'm fetching the posts like this:
current_user.posts
current_user.organization.posts
current_user.team.posts
How would I merge these queries returning a single AR object with all posts?
I have tried the following:
Post.where(trackable: current_user).where(trackable: current_user.team) # returns 0 objects
current_user.posts + current_user.organization.posts # this returns an array
This should do the trick:
Post.where(trackable: [current_user, current_user.organization, current_user.team])
I have a Shop model which has many users:
Shop.rb
def active_users
users.where(status: "active").reverse
end
Currently, when I call some_shop.active_users, I get an array of user objects. But is it possible to instead get an ActiveRecord collection of Users?
It's lazy binding. It'll be an array when you access it.
Try it by calling active_users.to_a
A Post has_many Tags. Each tag record has a name, like "fashion" or "business".
How can I get all posts that have the tag with name "fashion"?
# Get all posts
#posts = Post.all
# Get the filter tag
#filter_tag = Tag.find_by_name(params[:filter_name])
Ok so now we have all posts, and now I just want the #posts that have the #filter_tag. What should I do?
I could loop through each post, and check if it has the filter tag. If so, add it to an array. I'm sure Rails ActiveRecord has a better method though.
If you have your associations set up correctly then
#filter_tag_posts = #filter_tag.posts
I have a Model Form which has an has_many relationship to a model User. User belongs_to Form.
I'm trying to access the values in User from my HomeController:
#forms = Form.all
#forms_by_val = #forms.users.group_by(&:value)
But this is giving me the error. Any ideas how to fix?:
undefined method `users' for # Array:0x007fdb32672dd8>
#forms that is Form.all refer to the array that have Form instances. users method is only available to a Form instance. Not to an array of Form instances.
I think you are trying to do something like this.
#forms_by_val = #forms.map {|form| form.users }.flatten
This will give you an array of users that belong to individual forms.
If you want a unique list of users...
#forms_by_val = #forms.map {|form| form.users }.flatten.uniq
I have many photos that belong to Movie like Movie.photos. Some movies have photos some other not.
How can easily find all the movies with no photos?
Put this method in your Movie model
def self.no_photos
Movie.all.reject{|movie| movie.photos}
end
You can use it this way.
movies_with_no_photos = Movie.no_photos