Here is my data model:
class A
has n, :b
has n, :c, through => :b
def active_c
c.active
end
end
class C
class << self
def active
all(self.end_on => nil) + all(:conditions => [ "cs.end_on >
applied_to" ])
end
end
end
The the SQLs look like:
SELECT `cs`.`id` FROM `cs` INNER JOIN `bs` ON `cs`.`id` = `bs`.`c_id`
INNER JOIN `as` ON `bs`.`a_id` = `as`.`id` WHERE (`bs`.`a_id` = 1672
AND `cs`.`end_on` IS NULL)
SELECT `cs`.`id` FROM `cs` INNER JOIN `bs` ON `cs`.`id` = `bs`.`c_id`
INNER JOIN `as` ON `bs`.`a_id` = `as`.`id` WHERE (`bs`.`a_id` = 1672
AND (cs > applied_to))
SELECT `id`, `created_at`, `updated_at`, `applied_to`, `end_on` FROM
`cs` WHERE (1 = 0 OR 1 = 0) GROUP BY `id`, `created_at`, `updated_at`,
`applied_to`, `end_on` ORDER BY `id`
If I change the active_c to this impl:
def active_c
C.all(:bs => {:a_id => self.id}).active
end
The result SQLs for invoking a.active_c are:
SELECT `c_id` FROM `bs` WHERE `a_id` = 1670
SELECT `c_id` FROM `bs` WHERE `a_id` = 1670
SELECT `id`, `created_at`, `updated_at`, `applied_to`, `end_on` FROM
`cs` WHERE ((1 = 0 AND `end_on` IS NULL) OR (1 = 0 AND (cs.end_on >
applied_to))) ORDER BY `id`
Now two problems:
1. I have to use cs.end_on in the second condition for the *as* table
has also a column called end_on and DM will confuse
2. Why there are 3 queries and where the 1 = 0 coming from
Thanks
--
You received this message because you are subscribed to the Google Groups
"DataMapper" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/datamapper?hl=en.