Hi !
I have the following:
class Estimate
has_many :items, :class_name => ''EstimateLine'', :order
=> ''position''
end
class EstimateLine < ActiveRecord::Base
belongs_to :estimate
acts_as_list :scope => :estimate
end
class EstimateProductLine < EstimateLine
end
class EstimateCommentLine < EstimateLine
end
When I try to insert a comment and a product, I get an invalid SQL
statement:
ActiveRecord::StatementInvalid: #23000
Duplicate entry ''9252-1'' for key 2: INSERT INTO estimate_lines
(`estimate_id`, `product_id`, `quantity`, `type`, `comment`, `position`)
VALUES(9252, 104, 1, ''EstimateProductLine'', NULL, 1)
If I look at my table, I can see the comment, at position 1. Notice
ActiveRecord is trying to insert at position 1 again. If I check the
log, I can see where Rails is searching for the bottom element to
increment the position column:
EstimateCommentLine Load (0.000000) SELECT * FROM estimate_lines WHERE
estimate_id AND (estimate_lines.type = ''EstimateCommentLine'' )
ORDER BY
position DESC LIMIT 1
SQL (0.000000) INSERT INTO estimate_lines (`estimate_id`, `product_id`,
`quantity`, `type`, `comment`, `position`) VALUES(9226, NULL, 0,
''EstimateCommentLine'', ''Ropelight
Sculptures'', 1)
EstimateProductLine Load (0.000000) SELECT * FROM estimate_lines WHERE
estimate_id AND (estimate_lines.type = ''EstimateProductLine'' )
ORDER BY
position DESC LIMIT 1
You can see where EstimateCommentLine Load and EstimateProductLine Load
are done, ActiveRecord ensures the type is the same as the current
class, and not the class where the acts was defined... So I tried
changing my scope to:
''estimate_id = #{self.estimate_id} AND type =
\''EstimateLine\''''
But that doesn''t help, as ActiveRecord now tries to find type =
''EstimateLine'' AND type =
''EstimateProductLine'', which always returns 0...
So, how do I get myself out of this bind ? Should I just fold
everything up into a single class, or should I patch my ActiveRecord to
correct this problem ?
Thanks !
François