I am very new to Ruby (2-3 days) and am using it on an existing DB. I have a user table (previously built in Joomla) and have managed to get it working (well, logging in and out), but i need to constrain another model i.e. ‘Leads’ to the user that is logged in.
I have been able to get ALL the current ‘leads’ output and using CRUD operations, but i would like to know how to limit the data based on the user that is logged in.
The ‘leads’ model was generated using scaffolding and the ‘set_table_name’ and ‘set_primary_key’ attributes have been overridden. I have also added
belongs_to :user,
:class_name => "user",
:foreign_key => "leadid"
to the model.
The ‘user’ model is as follows
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
`establish_connection :web_development
set_table_name "users"
set_primary_key "id"
has_many :leads,
:class_name => "Lead",
:foreign_key => "LeadID"
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me
alias :devise_valid_password? :valid_password?
def valid_password?(password)
return true (temp to bypass the password)
end
end
Many thanks
Rudy
Devise provides an accessor to the currently logged in user with
current_user. Since it looks like you have the relationship set up correctly, if you want to show only the leads for the logged in user in your index action of the controller you just need this: