Hi Brian,
Thanks so much for the help. Please see below.
Brian H. wrote:
Try this:
@contacts = Contact.find_all_by_user_id session[‘user’].id
(We’ll use a dynamic finder here instead of conditions.)
@contacts = Contact.find_all_by_user_id session[‘user’].id
NameError: undefined local variable or method `session’ for
#Object:0x4ec91f8
from (irb):11
from :0
if I do it with @session…
@contacts = Contact.find_all_by_user_id @session[‘user’].id
NoMethodError: You have a nil object when you didn’t expect it!
You might have expected an instance of Array.
The error occured while evaluating nil.[]
from (irb):12
from :0
Also, you shouldn’t be using @session because it references a variable.
There’s a session method you should go through instead… see
http://weblog.rubyonrails.org/2006/4/25/use-params-not-params for more
information.
Thanks for the tip! I’ll keep that in mind.
And if you still get no luck, show me your contacts table (migration),
I had to run a few migrations before I got the table I wanted (my first
time with migrations–sorry!). First I added the table, then I got rid
of contact_id because I remembered that I get id for free, then I tried
to add a foreign key constraint, realized that I couldn’t and ended up
adding an index.
class AddContactsTable < ActiveRecord::Migration
def self.up
create_table :contacts do |t|
t.column :contact_id, :integer
t.column :contact_guid, :integer
t.column :contact_first_name, :string
t.column :contact_email, :string
t.column :user_id, :integer
t.column :in_address_book, :boolean
end
end
def self.down
drop_table :contacts
end
end
class RemoveContactId < ActiveRecord::Migration
def self.up
remove_column “contacts”, “contact_id”
end
def self.down
add_column “contacts”, “contact_id”, :integer
end
end
class AddFkToContacts < ActiveRecord::Migration
def self.up
remove_column :contacts, :user_id
add_column :contacts, “user_id”, :integer, :limit => 10, :default =>
0, :null => false
add_index “contacts”, [“user_id”], :name => “user_id_ind”
end
def self.down
remove_column :contacts, :user_id
add_column :contacts, “user_id”
remove_index :contacts, :name => :user_id_ind
end
end
So those are my embarassing migrations.
here’s what it looks like now in schema.rb:
create_table “contacts”, :force => true do |t|
t.column “contact_guid”, :integer
t.column “contact_first_name”, :string
t.column “contact_email”, :string
t.column “in_address_book”, :boolean
t.column “user_id”, :integer, :limit => 10, :default => 0, :null =>
false
end
add_index “contacts”, [“user_id”], :name => “user_id_ind”
the code where you save your user object to session,
I used the Salted Hash Login Generator. I thought it handles the
authentication stuff, including saving the user object to session. Is
there something else I need to do? Here’s the login method from the
generated user controller:
def login
return if generate_blank
@user = User.new(@params[‘user’])
if @session[‘user’] = User.authenticate(@params[‘user’][‘login’],
@params[‘user’][‘password’])
flash[‘notice’] = l(:user_login_succeeded)
redirect_back_or_default :controller => ‘blog’, :action => ‘index’
else
@login = @params[‘user’][‘login’]
flash.now[‘message’] = l(:user_login_failed)
end
end
and then show me your contacts model.
class Contact < ActiveRecord::Base
belongs_to :user, :foreign_key => “user_id”
end
The :foreign_key declaration is probably superfluous but it doesn’t seem
to make things worse.
Thanks!
Rick