Quote (Kamikizzle @ Jun 15 2014 04:31pm)
using your query (fixed some typos):
Code
SELECT cards.id, array_to_string(array_agg(cards.color_name), ',') as color_names
FROM cards
INNER JOIN cards_colors ON cards.id = cards_colors.card_id
INNER JOIN colors ON colors.id=cards_colors.color_id
WHERE array_to_string(array_agg(cards.color_name), ',') ='Green, Black'
GROUP BY cards.id
yields this error:
Code
PG::UndefinedColumn: ERROR: column cards.color_name does not exist
LINE 1: SELECT cards.id, array_to_string(array_agg(cards.color_name)...
because cards are related to colors with a has_and_belong_to_many relationship
scheema:
Code
create_table "cards", force: true do |t|
t.string "name", null: false
t.string "mana_cost"
t.integer "loyalty"
t.string "img_link"
t.float "converted_mana_cost"
t.text "oracle"
t.string "rarity"
t.integer "power"
t.integer "toughness"
t.integer "mtg_set_id"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "cards_colors", force: true do |t|
t.integer "card_id"
t.integer "color_id"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "colors", force: true do |t|
t.string "color_name"
t.datetime "created_at"
t.datetime "updated_at"
end
models (they have other non-related relations and validations):
Code
class Card < ActiveRecord::Base
has_and_belongs_to_many :colors, join_table: :cards_colors
end
class Color < ActiveRecord::Base
has_and_belongs_to_many :cards, join_table: :cards_colors
end
i don't know anything about Magic, so you'll have to explain what you want to return better.
Quote
basically, any card can have any permutation of 5 colors (blue, black, green, red, white) and i have a search form that needs to be able to search for mono-black cards, black-green cards but not return mono-green cards, or black-green-red cards etc.
i'm not clear how the colours work. you said any permutation is available. does that mean blue-black is different from black-blue? and this card can have both options? or did you mean to say any combination is available? eg blue-black is the same as black-blue?
if you meant any combination is available, and i'm guessing colours are constant, then i suggest you redesign your mapping table. eg, tablename = 'colours', columns = {cardid, blue, black, green, red, white}
then your query would be like so (example of all blue-green-white cards):
select * from cards
inner join colours on colours.cardid = cards.cardid
where colours.blue = 'Y' and colours.black = 'N' and colours.green = 'Y' and colours.red = 'N' and colours.white = 'Y'
i used separate varchar columns for clarity, but you can stick them all into a single column or use numerics/boolean if you wish. imo this design is the simplest and prolly performs best. your own design seems to overcomplicate it.
if you were correct by saying permutation instead of combination, then it'll be much different and i'll revisit it once you confirm. essentially your columns would be colour1, colour2, etc. and you'd query them in the order given. eg colour1 = 'blue' and colour2 = 'black', normalized as desired.
This post was edited by carteblanche on Jun 15 2014 03:02pm