2

I am trying to get a user who has not filled out their profile to redirect to the edit page.

I have two tables - Users (Devise handles) and Profiles.

Profiles has a row called profile_complete

In my user model I am trying to define a method called profile_complete which takes the boolean of profiles_complete and passes it back.

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable,
  # :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me

  has_many :profiles, :dependent => :destroy

  def profile_complete?
    self.profile.profile_complete == true
  end
end

But cannot seem to figure out what the line in the profile_complete method is. I have got the other bits working in the correct place but cant get this variable across Any help? Cheers :)

  • 3
    In virtually *every* instance, comparing something to `true` is pointless. You should just return that as-is. – tadman Jan 08 '13 at 16:04
  • 1
    Should you have a `has_one :profile` on your user ? Then you should be able to do `profile && profile.profile_complete` – bullfrog Jan 08 '13 at 16:05
  • They can have many profiles :) @bullfrog –  Jan 08 '13 at 16:22
  • does every profile related to the user have to be complete for that method to return true ? if so then `self.profiles.all? {|profile| profile.complete}` – bullfrog Jan 08 '13 at 16:25
  • @bullfrog, that will return true if there isn't an associated profile for a given user. for eaxmple: [].all? {false} #=> true. You would also need to co something like !self.profiles.empty? – Lukas Eklund Jan 08 '13 at 16:51
  • @LukasEklund thats a good point.. I suppose then can do `self.profiles.present? && self.profiles.all?(&:profile_complete)` – bullfrog Jan 08 '13 at 17:00
  • @bullfrog, that looks great, and it uses present instead of having to negate empty, which is much nicer. – Lukas Eklund Jan 08 '13 at 17:07

1 Answers1

1
def profile_complete?
    self.attributes.values.include?(nil) ? false : true
end
Eugene Rourke
  • 4,934
  • 1
  • 22
  • 24