lundi 24 août 2015

How can i use data from antoher tables in a active record where in rails

im trying to make a search form in a table that contains two ids as values, one of them is the id of an user and the other is the id of a project. Here is my database schema:

  create_table "users", force: :cascade do |t|
    t.string   "name"
    t.string   "email"
    t.text     "bio"
    t.float    "lastLatitude"
    t.float    "lastLongitude"
    t.datetime "created_at",    null: false
    t.datetime "updated_at",    null: false
    t.integer  "swipes"
    t.string   "profile_image"
 end

 create_table "projects", force: :cascade do |t|
   t.string   "name"
   t.text     "bio"
   t.integer  "user_id"
   t.string   "salary"
   t.datetime "created_at", null: false
   t.datetime "updated_at", null: false
   t.integer  "likes"
   t.string   "image"
   t.float    "latitude"
   t.float    "longitude"
 end

 create_table "matches", force: :cascade do |t|
  t.integer  "user_id"
  t.integer  "project_id"
  t.datetime "created_at", null: false
  t.datetime "updated_at", null: false
end

What i want to do is to create a search form in the matches index view to filter the matches by the user name or the project name, but i don't know how to do it, this is what i have right now:

 def self.search(search)
   if search
   where('User(user_id).name LIKE :search OR Project.name LIKE :search', search: "%#{search}%")
 else
   where(nil)
  end
end

Is it possible to do that? Any help is welcome

Aucun commentaire:

Enregistrer un commentaire