I need to get all the brands name for each category, I get it through
my machines which belong to a category and to a brand. The problem is
that I get double entries in my @brands hash. I tried @brands.uniq but
this is obviously not working as there are other values than name in
my @brands hash. Any idea how I could remove all the doubles from my
@brands?
What does the generated SQL look like? I'm not quite picturing where
the doubles are coming from.
David
ok for each category I want to get all the brands it has, here is what
the query is for Category.id=50:
SELECT brands.`id` AS t0_r0, brands.`name` AS t0_r1, machines.`id` AS
t1_r0, machines.`category_id` AS t1_r1, machines.`brand_id` AS t1_r2,
machines.`model` AS t1_r3, machines.`serial` AS t1_r4, machines.`type`
AS t1_r5, machines.`year` AS t1_r6, machines.`hours` AS t1_r7,
machines.`miles` AS t1_r8, machines.`capacity` AS t1_r9,
machines.`price` AS t1_r10, machines.`description_fr` AS t1_r11,
machines.`description_en` AS t1_r12, machines.`description_es` AS
t1_r13, machines.`description_ar` AS t1_r14, machines.`added_date` AS
t1_r15, machines.`update_date` AS t1_r16 FROM brands LEFT OUTER JOIN
machines ON machines.brand_id = brands.id WHERE (category_id=50)
no wait I still get double entries
this what my query looks like:
SELECT brands.* FROM brands INNER JOIN machines ON brands.id =
machines.brand_id WHERE (machines.category_id = 20)
What does the generated SQL look like? I'm not quite picturing where
the doubles are coming from.
ok for each category I want to get all the brands it has, here is what
the query is for Category.id=50:
SELECT brands.`id` AS t0_r0, brands.`name` AS t0_r1, machines.`id` AS
t1_r0, machines.`category_id` AS t1_r1, machines.`brand_id` AS t1_r2,
machines.`model` AS t1_r3, machines.`serial` AS t1_r4, machines.`type`
AS t1_r5, machines.`year` AS t1_r6, machines.`hours` AS t1_r7,
machines.`miles` AS t1_r8, machines.`capacity` AS t1_r9,
machines.`price` AS t1_r10, machines.`description_fr` AS t1_r11,
machines.`description_en` AS t1_r12, machines.`description_es` AS
t1_r13, machines.`description_ar` AS t1_r14, machines.`added_date` AS
t1_r15, machines.`update_date` AS t1_r16 FROM brands LEFT OUTER JOIN
machines ON machines.brand_id = brands.id WHERE (category_id=50)
Doesn't AR squish that down into one brand with multiple
eagerly-loaded machines? I've tried to do a mini-replica of your
domain model (using household appliances, which may or may not be your
actual domain On the SQL side:
> SELECT brands.`id` AS t0_r0, brands.`name` AS t0_r1,
machines.`id` AS t1_r0, machines.`name` AS t1_r1, machines.`brand_id`
AS t1_r2, machines.`category_id` AS t1_r3 FROM brands LEFT OUTER JOIN
machines ON machines.brand_id = brands.id WHERE (category_id = 1);
no wait I still get double entries
this what my query looks like:
SELECT brands.* FROM brands INNER JOIN machines ON brands.id =
machines.brand_id WHERE (machines.category_id = 20)
ok I did a quick hack:
def getbrands(cat)
@brands = Category.find(cat.id).brands
ar=
for b in @brands
ar.push b.name
end
return ar.uniq
end
You could write that as:
def getbrands(cat)
@brands = Category.find(cat.id).brands
@brands.map {|b| b.name }.uniq
end
and if you don't need the instance variable you can factor it out (or
make it local). However...
not perfect but it does the trick
I can't help wondering whether there's something engineered wrongly
that is going to show itself later. I'd still be interested in trying
to track it down, unless you've moved on to other things
I'm having some trouble with dates in ruby. I'd like to create a date
4 weeks ago from the current date, and find the date of the monday for that week.
I'm having some trouble with dates in ruby. I'd like to create a date
4 weeks ago from the current date, and find the date of the monday for
that week.