开发者

Ruby on Rails, Creating method

I'm trying to create a method to run af开发者_如何学Goter 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


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


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

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜