0

I have two models, Message and User. Message has two User objects, user_1 and user_2.

class Message < ActiveRecord::Base
  attr_accessible :user_1, :user_2

  belongs_to :user_1, :class_name => "User"
  belongs_to :user_2, :class_name => "User"
end

class User < ActiveRecord::Base
  attr_accessible :email, :password

  has_many :message, dependent: :destroy, :finder_sql => 'SELECT * FROM messages where (user_1_id = #{id} or user_2_id = #{id})'
end

When I try and delete the User object, I get the following error:

ActiveRecord::StatementInvalid in UsersController#destroy

SQLite3::SQLException: unrecognized token: "#": SELECT * FROM messages where (user_1_id = #{id} or user_2_id = #{id})

The error message seems plain enough, but I thought # was the correct syntax. I'm using Rails 3.2.3. Thanks!

4

1 に答える 1

3

In ruby everything inside single quotes except \\ and \' is treated literally.

You will have to use double quotes for your SQL statement.

于 2012-05-16T13:55:51.450 に答える