Ruby on Rails
Saturday, October 29, 2011
On Sat, Oct 29, 2011 at 4:31 AM, Colin Law <clanlaw@googlemail.com> wrote:
-- On 28 October 2011 22:46, Christopher J. Bottaro <cjbottaro@gmail.com> wrote:Not answering the question I am afraid, but I think it is unwise to
> Hello,
> What's the best/easiest way to write this delete statement using
> ActiveRecord 3.1?
>
> DELETE e1
> FROM events e1
> JOIN events e2
> WHERE e1.subject_type = e2.subject_type
> AND e1.subject_id = e2.subject_id
> AND e1.origin_type = e2.origin_type
> AND e1.origin_id = e2.origin_id
> AND e1.id > e2.id
assume anything about the id sequence. Presumably here you are
assuming that id values are assigned in an increasing sequence, but I
don't think this is necessarily guaranteed in the general case. I
think it might be better to use created_at, if that is what you really
mean. On the other hand if in reality you do not care which one you
delete and have the id test only to make sure that you delete only one
of them then please ignore my comment.
Since you are interested in the best way to code it (rather than just
hacking in the sql) then presumably it is something that happens
routimnely rather than some tidying up operation that you have to do
once. Would it not be possible using validations or similar to ensure
that the duplicate record situation does not happen in the first
place?
Colin
Hmm. Interesting. On the one hand, I'm glad people are looking out for each other and advice is given on best practices. On the other hand, I forgot what it's like to ask for help on the internet and have everything you do under heavy scrutiny… :)
Presumption incorrect. It is a one off and not routine code, but that doesn't stop me from wanting to learn how to better use AR/Arel. Also, I created a unique index as soon as I realized there were dupes and I cleaned them out. I also added validation (which isn't guaranteed to work, hence the unique index in the db), and test cases/specs for the situation.
About the id vs created_at… I disagree and consciously chose the former. I think either will work fine and it's ok to make assumptions about the uniqueness (for sure) and order (comfortably sure) of primary keys for a given adapter. I'm familiar with the Postgres and MySQL adapters and I know they create unique, auto incrementing primary keys for each table.
That said, I am open to notion that I'm wrong or do not fully understand something though, so…
Thanks for the help,
-- C
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment