1

I'm trying to create a method to run after any new user is created. Its been a while since I've used ruby so am having some trouble.

I get this error C:/Users/antarr/SeniorProject/app/models/user.rb:9: syntax error, unexpected tASSOC, expecting keyword_then or ';' or '\n'

class User < ActiveRecord::Base

  acts_as_authentic
  after_create :set_universal
  after_create :set_carrier
  after_create :set_role

  def set_role
    if User.count >= 1
      endself.roles << "admin"
    else
      self.roles << "subscriber"
    end
  end

  def set_universal
    if Channel.find(1).exist
      self.channels << Channel.find(1)
  end

  def set_carrier
    self.carrier = Carrier.with_name(self.carrier_name).first
  end

  ROLES = %w[admin  moderator subscriber]

  #Each user can subscribe to many channels
  has_and_belongs_to_many :channels

  #Each user who is a moderator can moderate many channels
  #has_many :channel_mods
  has_and_belongs_to_many :modifies , :class_name => "Channel"

  #Each user can receive many messages
  has_and_belongs_to_many :messages

  #Each user belongs to a carrier
  belongs_to :carrier

  #Filter users by role(s)
  named_scope :with_role, lambda { |role| {:conditions => "roles_mask & #{2**ROLES.index(role.to_s)} > 0 "} }

  def roles  
    ROLES.reject { |r| ((roles_mask || 0) & 2**ROLES.index(r)).zero? }  
  end

  def roles=(roles)  
    self.roles_mask = (roles & ROLES).map { |r| 2**ROLES.index(r) }.sum  
  end

  def role_symbols
  roles.map do |role|
    role.underscore.to_sym  # NOT role.name.underscore.to_sym (role is a string)
    end
  end


end

2 Answers 2

4

You have the operators wrong, it's >=, not =>. => is a operator for Hashes.

def set_role
  if User.count >= 1
    self.roles << "admin"
  else
    self.roles << "subscriber"
  end
end
Sign up to request clarification or add additional context in comments.

6 Comments

would this work ? def set_role begin self.roles << "admin" rescue ensure self.roles << "subscriber" end end
why do you need a begin/rescue block? The code in my answer works fine.
Can you explain what you're trying to accomplish?
im still getting this error C:/Users/antarr/SeniorProject/app/models/user.rb:25: dynamic constant assignment ROLES = %w[admin moderator subscriber] ^ C:/Users/antarr/SeniorProject/app/models/user.rb:58: syntax error, unexpected $end, expecting keyword. I will post entire model in case I am missing something
Edit your question and include the entire user.rb class.
|
3

"Hash rockets" (=>) are used for hashes in Ruby, you need >=.

...
if User.count >= 1
...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.