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