join in rails 4 with scope - ruby-on-rails

I have Blog and Category models in my rails 4 application. there is a many to many relationship between these two models. i have multiple checkbox. I want to get all the blogs which belongs to that category. I have this in my Blog model
scope :by_categories, lambda{|category_ids| joins(:blog_categories).where("blog_categories.category_id in (?)", category_ids) if category_ids.present?}
and this in my controller
def search_blogs
#blogs = Blog.by_categories(params[:category_ids])
end
but whenever i choose multiple categories like category_ids => [1,2,3] , I am getting blogs for only category_id 1 and not for 2 and 3

Since, you are getting blogs having by categories. So use following code:
scope :by_categories, lambda{|category_ids| joins(:blog_categories).where("blogs.category_id in (?)", category_ids) if category_ids.present?}

Related

Rails, Spree: How to check if a product belongs to a parent category

I have the following structure in spree:
Taxon aTaxon a.1Taxon a.1.1 Product 1 Product 2 Product 3
I need to know if there is a optimal way to list all the taxons of the products including the parents.
Thanks!.
If you want to list the taxons a product belongs to (I'm not clear if that's what you're after), an option is to take advantage of helper methods in the acts_as_nested_set functionality that Spree uses for hierarchical taxons.
product = Spree::Product.includes(:taxons).find_by(slug: 'gift-card')
product.taxons.map { |taxon| taxon.self_and_ancestors.map(&:name).join(' > ') }
=> ["Christmas Promotions > Last Minute > Gift Cards", "Digital Products > Gift Cards"]
Have a look at all the methods available for nested sets for other ideas of how you can do this and similar things: https://github.com/collectiveidea/awesome_nested_set/wiki/Awesome-nested-set-cheat-sheet
I found a good approach in the following post: Spree: intersection between taxons
Adding a search scope to the prodcut model:
add_search_scope :in_taxon do |taxon|
Spree::Product.joins(:taxons).where(Taxon.table_name => { :id => taxon.id }).
order("spree_products_taxons.position ASC")
end
Then:
Spree::Product.all.in_taxon(Spree::Taxon.find_by_name('Taxon a'))
Will list all the products inside the referred taxon.

Get children of a collection of parents with ActiveRecord

I have a Category model and Post model with a one-to-many relationship.
I'd like to have all posts for a certain set of categories.
I want the result to be an ActiveRecord object to be able to do further queries.
Right now I'm using .map like so
categories.map{|c| c.posts.order(position: :asc)}
Use embedded query to Posts, as follows:
Post.where(category_id: Category.all.pluck(:id)).order(position: :asc)
First find all the categories that you are interested and get the ids:
category_ids = Category.where('name like ?', '%foo%').pluck(:id)
Then just query for the Posts where the category_id is included in this list of ids:
posts = Post.where(category_id: category_ids)
This is a AR object, so you can keep adding order or where and so on:
posts.order(position: :asc)

Rails ActiveRecord : search in multiple values with multiple values

So let's say I have Post, Category and Categorizations models.
A post can have many categories through categorizations.
Now, how can I pull out all the posts that match at least one item of an array of categories?
Example:
Post 1 has categories 2,5,6
Post 2 has categories 1,5,9
Post 3 has categories 2,4,8
Find posts that match 3,5
I want the posts 1 and 2 to be returned.
Thanks!
Assuming that Categorization is a join model for Post and Category:
Post.joins(:categorizations).where(:categorizations => {:category_id => [3, 5]})
If it's not, and Categorization actually has_many :categories then:
Post.joins(:categories).where(:categories=> {:id => [3, 5]})
Note that the second method will work in the first case as well, however it will require 2 SQL joins and thus may not perform as well.

rails 3 order by another models column

I have 2 models in my Rails 3 app which I use to describe people and where they live
Unfortunately I set these up without using associations
The 2 tables are setup like this
People
id
name
location_id
Locations
id
name
what I want to do is list all entries in the Peoples table ordered by Locations.name alphabetically and People.name alphabetically
I can do a simple sort using this code which groups each person by a location but I need to drill into the Locations table as well
#people = People.all(:order => '"location_id" ASC, "name" ASC')
Anyone have any idea?
Also is it a good idea to set up an association in the People class to say location_id is Locations.id
Add
belongs_to :location
To the People class
Then you can query the following way:
#people = People.joins(:location).order("locations.name ASC, people.name ASC")

Advanced friendship model and queries in Rails 3

I have friendship model, and for every new friendship, I create two new records:
User1 and Friend1
Friend1 and User1
I can retrieve all standard staffs like: friends, friendships, pending_friends... The situation becomes complicated when I try to get common friends, friends of friends...
For now to get common friendships, I use something like:
has_many :common_friendships, :class_name => 'Friendship', :conditions=>'friendships.user_id = #{self.id}' do
def common_with(friend)
joins("inner join friendships fl2 on friendships.friend_id = fl2.user_id").where("fl2.friend_id = #{friend.id}")
end
end
Also I can use full query with finder_sql like:
select distinct *
from friendships fl1
inner join friendships fl2 on fl1.friend_id = fl2.user_id
where fl1.user_id = 1 and fl2.friend_id = 2
How can I do that in an elegant way in Rails 3?
Check out the answer on this question:
How to Implement a Friendship Model in Rails 3 for a Social Networking Application?

Resources