1

i have two model 'Asset' , and 'User' .'Asset' can be assigned to one 'user' and 'asset' is created by one 'user'.here is detail of model classes

class Asset < ActiveRecord::Base
belongs_to :assigned_to ,:class_name=>'User'
belongs_to :creator ,:class_name=>'User'
end

and

class User < ActiveRecord::Base
has_many :assets
end

now in assets show.html.erb i can access name of creator with

 @asset.creator.name

but i can not see name of 'assigned_to'

@asset.assigned_to.name  #gives nothing

both values are successfully saved in database.what is problem?

Naveed
  • 11,057
  • 2
  • 44
  • 63

2 Answers2

0

Try printing the values directly to console:

puts @asset.pretty_inspect
puts @asset.assigned_to.pretty_inspect

Something;s not right :)

adzdavies
  • 1,545
  • 1
  • 13
  • 13
  • undefined method `pretty_inspect' for # – Naveed Mar 22 '11 at 15:15
  • i am new to rails i have tries in console and i thins all is well in console. console result is user=User.new=> #\n user.id=2 user.name='foo' user.inspect=>"# asset=Asset.new :name='mobile'=> # asset.creator=user=># means fine asset.assigned_to=user=> same as above ok now asset.creator.name=>"foo" asset.assigned_to.name=>"foo" i think in console we explicitly assign value of relation so works and in development ActiveRecord (am i right?) do this job. – Naveed Mar 22 '11 at 15:16
  • First -- don't assign .id = 2 yourself, ActiveRecord will do it for you when you save: "user.save!". Second -- you must do the assign in the rails app the same way as the console. However, if you've generated the scaffold you'll see something like "User.update_attributes(params[:user])" which will contain all the form fields. You just need to ensure you have the form-field for :assigned_to added to your form. Something like: <%= select("asset", "assigned_to_id", User.all.collect {|p| [ p.name, p.id ] }, { :include_blank => true }) %> – adzdavies Mar 22 '11 at 22:30
  • ...and if you're already doing something like that, my original answer applies -- just output it in the view to trace -- "<%= @asset.assigned_to.inspect %>" – adzdavies Mar 22 '11 at 22:32
  • thanks for reply yes assigned_to is present and its value is also saved in database with other form fields.my question was as we can access name of creator by @asset.creator.name but i can't access name of assigned_to user by @asset.assigned_to.name it give error undefined method `first_name' for nil:NilClas i also tries @asset.assigned_to_id.name=>(error is) undefined method `first_name' for 1:Fixnum . i think there is some problem with association b/w user and asset model. – Naveed Mar 24 '11 at 11:51
  • my models are class Asset < ActiveRecord::Base { //validation belongs_to :creator ,:class_name=>'User' belongs_to :assigned_to, :class_name=>'User' ,:foreign_key=>'assigned_to_id' } and class User < ActiveRecord::Base { //any validation and other stuff has_many :assets #did not specify either this association #is for creator , or assigned to how can is specify this?? } now problem is to specify two has_many :assets in User member on for creator and other for assigned to what is syntax for that or a better solution – Naveed Mar 24 '11 at 11:54
0

finally my problem is solved here is solution

class Asset < ActiveRecord::Base

belongs_to :creator ,:class_name=>'User'
belongs_to :assigned_to, :class_name=>'User' 

end

and

user.rb

class User < ActiveRecord::Base

has_many :created_assets, :foreign_key => 'creator_id', :class_name => 'Asset'
has_many :assigned_assets , :foreign_key => 'assigned_to_id', :class_name => 'Asset'

end
Naveed
  • 11,057
  • 2
  • 44
  • 63