alfa alfa - 5 months ago 10
Ruby Question

self method not working on my rails application

I don't know why self method not working, if I declare self method on my model like

self.search
& controller
Post.search
& run the code then showing
undefined method search
.

Model:

class Post < ActiveRecord::Base
belongs_to :companies, :foreign_key => :company_id
include Tire::Model::Search
include Tire::Model::Callbacks

def self.search(q)
where("title LIKE ?", "%#{q}%")
where("description LIKE ?", "%#{q}%")
end
end


Controller:

def searchResult
@articles = Post.search(params[:q])
end


I'm suffering this last three or four days & until now.

Greatly appreciated if anyone help.

Thanks

Answer

I think I understood your problem, same as to me many days ago, lot of troubled but unfortunately I have created two classes like, I have two model

One Post

class Post < ActiveRecord::Base
end

Two User

class User< ActiveRecord::Base
end

class Post < ActiveRecord::Base
end

Unfortunately Post Class override on User Model, then when I work on self.method on PostModel then not work, show Undefined method search or anything. suddenly I removed (From UserModel remove PostClass) this & work fine.

You make sure this PostClass not override on any other model.

I think will help you.

Comments