samedi 30 janvier 2016

Querying the database passing multiple parameters rails

I have a user table and an activity table. The user has many activities. This is what i have in my users table:

class SorceryCore < ActiveRecord::Migration
  def change
create_table :users do |t|
    t.string :first_name
    t.string :surname
    t.integer :previous_award
    t.integer :chosen_award
  t.string :email,            :null => false
  t.string :crypted_password
  t.string :salt

  t.timestamps
end

add_index :users, :email, unique: true
end

This is what I have in my activities table:

class CreateActivities < ActiveRecord::Migration
  def change
create_table :activities do |t|
  t.integer :activity_type
  t.string :activity_name
  t.string :approver_email
  t.references :users, index: true, foreign_key: true

  t.timestamps null: false
end
end

In my view I want to show the activity_name, where user_id = the current user's id, and where the the activity_type = 1. I'm not sure where to write this method or how to call it either. I have read through the following link but can't seem to get anything working. http://ift.tt/Mfyo5w

I think I need to use something along the lines of this:

Activity.where("activity_type <= ?", 1).where("user_id <= ?", current_user.id)

But I'm not sure if this is supposed to go into a method in the controller or the model, and I'm not sure which controller or model it's supposed to go into

Aucun commentaire:

Enregistrer un commentaire