2 Replies - 2287 Views - Last Post: 12 December 2011 - 12:22 PM Rate Topic: -----

#1 NotarySojac  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 53
  • View blog
  • Posts: 428
  • Joined: 30-September 10

has_one relationship usage

Posted 11 December 2011 - 08:13 PM

Edit: Forgot to mention that this is a rails problem

Hey, I'm trying to toss together an application and I've been stuck on this part for a while.

I have a user object that looks like

User.email
User.password

etc...

I'm trying to get...
User.employee.blah



...thatched onto it, so my code looks more beautiful.

Employee will look like...
Employee.dob
Employee.position_title
Employee.origindate
Employee.departure_date




Here's my working console output, so I must be on the right track
root@sweets:/home/kentos/dev/rails/xpd3# r c
Loading development environment (Rails 3.0.7)
ruby-1.9.2-p180 :001 > u = User.last
 => #<User id: 100, email: "example-99@railstutorial.org", encrypted_password: "e3f71a9a44686493130023e303473a8a46a2ed3f7b6c714c2d8...", created_at: "2011-11-26 14:35:11", updated_at: "2011-11-26 14:35:11", salt: "89173dc006c3f5a668cdb537538c7d6aeaf3610c6a387149cd1...", admin: false>
ruby-1.9.2-p180 :002 > u.employee = Employee.new
 => #<Employee id: 5, employeeid: nil, name: nil, position_title: nil, origin_date: nil, dob: nil, created_at: "2011-12-12 02:59:23", updated_at: "2011-12-12 02:59:23", user_id: 100>
ruby-1.9.2-p180 :003 >




But my controller chokes =(

My Models look like this
(app/models/user.rb)
class User < ActiveRecord::Base
  attr_accessor :password
  attr_accessible :email, :password, :password_confirmation

  has_one :employee, :dependent => :destroy
  .
  .
  .
end



(app/models/employee.rb)
class Employee < ActiveRecord::Base
  attr_accessible :name, :email, :positiontitle, :dob, :origindate
  belongs_to :user
end




And my controller looks like this:
class EmployeesController < ApplicationController

  def index
    @employee = Employee.new
  end

  # When the user clicks "Make User employee" this method fires up
  # And sets it up
  def create
    # Get User by username...
    @user =  User.where(:email => params[:user][:email])

    # Make sure user is not already set to an employee
    #@employee = Employee.new

    e = Employee.new

    #p e
    @user.employee = e #create()#params[:employee])

    # print "Employee Created!"

  end

  def destroy
  end
end




This is the error I get while it's trying to get through employees#create
undefined method `employee=' for #<ActiveRecord::Relation:0xa093684>


This post has been edited by NotarySojac: 11 December 2011 - 08:21 PM


Is This A Good Question/Topic? 0
  • +

Replies To: has_one relationship usage

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2118
  • View blog
  • Posts: 3,244
  • Joined: 21-June 11

Re: has_one relationship usage

Posted 12 December 2011 - 10:57 AM

User.where(:email => params[:user][:email]) returns a collection of users (well, an active record relation), not a single user. You probably want User.where(:email => params[:user][:email]).first.
Was This Post Helpful? 1
  • +
  • -

#3 NotarySojac  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 53
  • View blog
  • Posts: 428
  • Joined: 30-September 10

Re: has_one relationship usage

Posted 12 December 2011 - 12:22 PM

That did the trick great. Thanks for your help. I was hopelessly halted by this one!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1