Help with model :conditions

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}'
...

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](http://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

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Ƒa8df27585ace3

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?

Sadly, no dice:

Cannot visit Proc

Extracted source (around line #12):
....
12: <%= f.association :redirect_to_page %>
....

<%= f.association :redirect_to_page %>

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.

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.

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.

What version of Rails are you using? The following code works fine for me:

Schema:

create_table :pages do |t|

t.string :name

t.integer :redirect_to_page_id

t.timestamps

end

Model:
class Page < ActiveRecord::Base

attr_accessible :name, :redirect_to_page_id

belongs_to :redirect_to_page,

class_name: “Page”,

foreign_key: :redirect_to_page_id,

conditions: proc{[‘id != ?’, self.id]}

end

In Rails Console:

Page.create(name: “Foobar”, redirect_to_page_id: 1)

Page.create(name: “Barfoo”, redirect_to_page_id: 2)

Page.find(1).redirect_to_page

=> nil

Page.find(2).redirect_to_page

=> <Page id: 1, name: “Foobar”, redirect_to_page_id: 1, …>

Does that work for you?

Btw, :page_to_redirect_to would probably be a better name than :redirect_to_page as the later sounds like an instruction not a name.

Let me know how you get on.

Jeremy Walker