model issues with ruby on rails 3

@user = User.find_by_user_name(user_name)
  if !@user.business_admins.nil?
        puts "Am here"
       @business_admin = @user.business_admins[0]
  end

class User < ActiveRecord::Base has_many :business_admins end class BusinessAdmin < ActiveRecord::Base belongs_to :user end Here what i am trying, I am using Mozilla rest-client, basically here the user_name found in the User table, But i didn’t put that id in the BusinessAdmin, So what the problem is when i run the code, this if !@user.business_admins.nil? will return true, because am getting this **“Am here”**Have any issues with this model??.. Thankyou Vishnu

I am not sure what you are saying. If you have user has_many business_admins then @user.business_admins will never be nil. It may be an empty array however. Perhaps you should be testing for the array empty rather than nil.

Colin

> @user = User.find_by_user_name(user_name) > if !@user.business_admins.nil? > puts "Am here" > @business_admin = @user.business_admins[0] > end > class User < ActiveRecord::Base > has_many :business_admins > end > > class BusinessAdmin < ActiveRecord::Base > belongs_to :user > end > > Here what i am trying, I am using Mozilla rest-client, basically here > the > user_name found in the User table, But i didn't put that id in the > BusinessAdmin, So what the problem is when i run the code, this if > !@user.business_admins.nil? will return true, because am getting this > "Am > here"

>I am not sure what you are saying. If you have user has_many >business_admins then @user.business_admins will never be nil. It may >be an empty array however. Perhaps you should be testing for the >array empty rather than nil.

Colin

Basically, the BusinessAdmin doesn't have that use_name's id, then why this checking happned?

First what relationships have you got between the models (has_many, belongs_to or whatever)?

As I said last time, if user has_many business_admins then @user.business_admins will always (effectively) be an array, which may be empty. It will never be nil.

By the way, you sent your last email to me instead of to the list. Please make sure you reply to the list.

Colin

First what relationships have you got between the models (has_many, belongs_to or whatever)?

As I said last time, if user has_many business_admins then @user.business_admins will always (effectively) be an array, which may be empty. It will never be nil.

@user = User.find_by_user_name(user_name)
  if !@user.business_admins.nil?
        puts "Am here"
       @business_admin = @user.business_admins[0]
  end

class User < ActiveRecord::Base has_many :business_admins end

class BusinessAdmin < ActiveRecord::Base belongs_to :user end

Here what i am trying, I am using Mozilla rest-client, basically here the user_name found in the User table, But i didn’t put that id in the BusinessAdmin, So what the problem is when i run the code, this if !@user.business_admins.nil? will return true, because am getting this “Am here” I am not sure what you are saying. If you have user has_many business_admins then @user.business_admins will never be nil. It may be an empty array however. Perhaps you should be testing for the array empty rather than nil. Colin

Basically, the BusinessAdmin doesn’t have that use_name’s id, then why this checking happned? if !@user.business_admins[0].nil? puts “Am here” @business_admin = @user.business_admins[0] end

if !@user.business_admins.empty? might be a bit cleaner and easier to understand, or if @user.business_admins.count == 0

Colin