*class SystemAdmin < ActiveRecord::Base*
* belongs_to :user*
*end*
*
*
*class User < ActiveRecord::Base*
* has_secure_password*
* *
* has_one :system_admin *
*end*
*
*
*@user = User.find_by_user_name(user_name)*
* if !@user.system_admins.nil?*
* puts 'am a sys_admin'*
*
*
The above code gives error like this
*
*
NoMethodError (undefined method `system_admins' for #<User:0xa81a2e0>):
app/controllers/application_controller.rb:17:in `user_authenticate'

Have any problem with this condition checking* if
!@user.system_admins.nil? *also any problem with the model relation?
*
*
*
*
*
*
*Thank you*
*vishnu*
*
*
* *

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/FhRZVfzMTtMJ.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Search Discussions

  • Michael Pavling at Apr 3, 2012 at 8:21 am

    On 3 April 2012 09:12, amvis wrote:
    class User < ActiveRecord::Base
    has_one  :system_admin
    end

    @user = User.find_by_user_name(user_name)
    if !@user.system_admins.nil?
    puts 'am a sys_admin'

    The above code gives error
    Since you've defined the association as a has_one, you should use it
    in the singular:
    if !@user.system_admin.nil?

    but "!...nil?" is a bit stinky; either use:
    if @user.system_admin
    or
    unless @user.system_admins.nil?

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Amvis at Apr 3, 2012 at 8:31 am
    Thanks, got the solution...
    On Tuesday, 3 April 2012 04:20:50 UTC-4, pavling wrote:
    On 3 April 2012 09:12, amvis wrote:
    class User < ActiveRecord::Base
    has_one :system_admin
    end

    @user = User.find_by_user_name(user_name)
    if !@user.system_admins.nil?
    puts 'am a sys_admin'

    The above code gives error
    Since you've defined the association as a has_one, you should use it
    in the singular:
    if !@user.system_admin.nil?

    but "!...nil?" is a bit stinky; either use:
    if @user.system_admin
    or
    unless @user.system_admins.nil?
    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/5om_ZR_2mOEJ.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedApr 3, '12 at 8:13a
activeApr 3, '12 at 8:31a
posts3
users2
websiterubyonrails.org
irc#RubyOnRails

2 users in discussion

Amvis: 2 posts Michael Pavling: 1 post

People

Translate

site design / logo © 2022 Grokbase