I think :conditions => proc { ['id != ?', id]} is correct, as the
WHERE statement is running on the "foreign" table (although I tried it
with redirect_to_page and get the same error).

Do you have an example of a working model and query I could test
with?  Maybe there's just something wrong with my configuration.

Thank you again for all your help.

On May 14, 1:22 pm, Jeremy Walker <jez.wal...@gmail.com> wrote:
> On 14 May 2012 18:16, Tuckie <witu...@gmail.com> wrote:
>
> > Sadly, no dice:
>
> > Cannot visit Proc
>
> > Extracted source (around line #12):
> > ....
> > 12:   <%= f.association :redirect_to_page %>
> > ....
>
> >  <%= f.association :redirect_to_page %>
>
> Should it redirect_to_page_id instead of id?
> :conditions => proc {['redirect_to_page_id  != ?', id]}
>
> I've checked this on a standard relationship without the class_name and
> foreign_key and it works perfectly.
>
>
>
>
>
>
>
> > On May 14, 10:23 am, Jeremy Walker <jez.wal...@gmail.com> wrote:
> > > On 14 May 2012 15:13, Tuckie <witu...@gmail.com> 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.
> > ..
>
> > > 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 <jez.wal...@gmail.com> wrote:
> > > > > On 14 May 2012 14:11, Tuckie <witu...@gmail.com> 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
> > rubyonrails-talk@googlegroups.com
> > > > .
> > > > > > To unsubscribe from this group, send email to
> > > > > > rubyonrails-talk+unsubscr...@googlegroups.com.
> > > > > > 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 rubyonrails-talk@googlegroups.com
> > .
> > > > To unsubscribe from this group, send email to
> > > > rubyonrails-talk+unsubscr...@googlegroups.com.
> > > > 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 rubyonrails-talk@googlegroups.com.
> > To unsubscribe from this group, send email to
> > rubyonrails-talk+unsubscr...@googlegroups.com.
> > 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 rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to