Question

Completely stuck on this and could use some help!

I'm having users input certain preferences and then showing each user a set of results based on those preferences.

There is a HABTM relationship between users and these preferences:

User.rb

has_many :userpreferences
has_many :preferences, through: :userpreferences

Preferences.rb

has_many :userpreferences
has_many :users, through: :userpreferences

Userpreferences.rb

belongs_to :user
belongs_to :preference

Now, preferences have an attribute called "tags" that list out certain details: i.e., large, small, thick, etc.

I want to show each user items based on their preferences (i.e based on the tags of the preferences they pick).

Right now I am trying:

current_user.preferences.all.each do |p|
        query = p.tags.split(' ')
            @items = Item.where{title.like_any query}

Now, for some reason, this only queries the tags for the first preference.

For example, if tags for preference 1 were: ["black", "leather", "suede"] and tags for preference 2 were: ["brown", "cloth"].......only the first set is being passed as the query.

Was it helpful?

Solution

Figured it out - for anyone interested, this had nothing to do with Squeel!

Just how I was organizing my loop before the query.

Solution!

query = []
      current_user.preferences.each do |p|

        query << p.tags.split(' ')
        end
            @items = Item.where{title.like_any query}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top