On 14 May 2012 15:13, Tuckie <[email protected]> wrote:
> Thanks for the quick response, but that gives me: undefined method
> `id' for #<Class:0x000000031017a8>
>
> I was under the belief that the single quotes were needed as per:
>
> https://groups.google.com/group/rubyonrails-talk/msg/d70a164a4156b70f?hl=en%C6%91a8df27585ace3
Ok, yeah. You are right. That's a very old thread and I suspect this many
have changed now. It was a very ugly workaround.
You can now use a proc, so
:conditions => proc {['id != ?', id]}
Does that work for you?
>
>
>
> On May 14, 9:20 am, Jeremy Walker <[email protected]> wrote:
> > On 14 May 2012 14:11, Tuckie <[email protected]> wrote:
> >
> >
> >
> >
> >
> >
> >
> >
> >
> > > I'm working on creating a model that allows redirect to another
> > > instance of the same type (Page). For obvious reasons, I don't want
> > > it to be able to redirect to itself. How can I get the model to
> > > properly filter this relationship? I'm currently getting the error:
> >
> > > Mysql2::Error: You have an error in your SQL syntax; check the manual
> > > that corresponds to your MySQL server version for the right syntax to
> > > use near '' at line 1: SELECT `pages`.* FROM `pages` WHERE (id !=
> > > #{self.id})
> >
> > > From what I've read, it should be replacing #{self.id} with the
> > > current instance.
> >
> > > class Page < ActiveRecord::Base
> > > has_ancestry
> > > belongs_to :footer,
> > > :foreign_key => 'footer_id',
> > > :class_name => 'ReusableText'
> > > belongs_to :redirect_to_page,
> > > :foreign_key => 'redirect_to_page_id',
> > > :class_name => 'Page',
> > > :conditions => 'id != #{self.id}'
> >
> > Use double quotes, not single quotes around "id != #{self.id}"
> >
> > You might be better doing the following instead to avoid SQL injection:
> > :conditions => ['id != ?', self.id]
> >
> > Jeremy Walker
> >
> >
> >
> >
> >
> >
> >
> > > ...
> >
> > > --
> > > 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 [email protected]
> .
> > > To unsubscribe from this group, send email to
> > > [email protected].
> > > For more options, visit this group at
> > >http://groups.google.com/group/rubyonrails-talk?hl=en.
>
> --
> 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 [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/rubyonrails-talk?hl=en.
>
>
--
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 [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.