ruby on rails - Not able to access related objects fields -


so i'm new rails , i'm not entirely sure i'm doing wrong. i've read says i'm doing right.

i have relationships between 2 models.

class photo < activerecord::base      has_many :votes      belongs_to :user   end  

and

class user < activerecord::base   attr_accessible :username, :email, :password, :password_confirmation, :remember_me   has_many :votes   has_many :photos end 

here controller methods

def index    @photos = photo.order("created_at desc").to_a  end    def create     @photo = photo.new(params[:photo])     @photo.user_id = current_user.id     if !@photo.save       @error = @photo.errors.full_messages.join('. ')       render view_for_new       return     end   end 

i know relationship works because in view when this: <%= photo.user %> user object back, , when <%= photo.user.inspect %> shows expected fields correct keys , values.

however want access fields such username, email, etc , display on page. how do this? i've tried doing <%= photo.user.email %> , other fields available doesn't seem working

alright figured out, or @ least partially.

instead of <%= photo.user.email %> did <%= photo.user.try(:email) %> , brought correct attribute looking for. seems association done correctly. don't know why <%= photo.user.email %> doesn't work, everywhere on line seems use sort of syntax.


Comments

Popular posts from this blog

php - Admin SDK -- get information about the group -

dns - How To Use Custom Nameserver On Free Cloudflare? -

Python Error - TypeError: input expected at most 1 arguments, got 3 -