last day (14 days later) » 

3:04 AM
0
Q: Displaying info contained in a tablet it "belongs to" and putting it in a view loop

KalcoderI've searched for this answer and can't find it anywhere in stack overflow. Here's my problem. A gurgle belongs to user. A user has many gurgles I want to be able to display in a loop, the data from another table that it has a active record relation to. When I display gurgles own index.html....

 
you can access user object based on gurgles object
 
Where and how do i do that?
 
@gurgle.user.email in your index view instead of @user.email
 
Doesn't work. Now I get undefined method `user' when I use that in index.html.erb
 
can you check Gurgle.first.user in console?
 
3:04 AM
I get the following Gurgle Load (0.3ms) SELECT "gurgles".* FROM "gurgles" ORDER BY "gurgles"."id" ASC LIMIT ? [["LIMIT", 1]] => nil ....... wouldn't no method suggest its not lack of a record? or am I reading the meaning of no method error wrongly?
 
Gurgle contain foreign_key of user
 
Yes its in the schema, t.index ["user_id"], name: "index_gurgles_on_user_id"
 
Please verify it with Gurgle record does it contain user_id column
 
I checked by going in console Gurgle.find(1) and it does have the column, the first record just has nil....
Gurgle Load (0.3ms) SELECT "gurgles".* FROM "gurgles" WHERE "gurgles"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
=> #<Gurgle id: 1, status: "Narghhhdfedfewfewf", user_id: nil, created_at: "2017-01-01 12:55:54", updated_at: "2017-01-02 02:03:11">
 
so it doesn't have user reference key
 
3:14 AM
Or maybe if that is causing the issue, that the first record is nil, I can either delete it or put code in to ignore it and move onto the next if its nil, is this whats causing the no method error?
 
just make one Gurgle record with specific user_id and check it.
yes check @gugle.user.nil?
<%=gurgle.user.emaiil unless gurgle.user.nil?%>
put this in your view file
 
Holy crap that worked!!!
 
great
 
So having nil foreign key id creates a no method error
 
what you do?
 
3:19 AM
I put in your conditional code unless nil
 
yes
good
 
so really either I delete all the records that have nil id or put in that conditional, awesome
 
are you freelancer?
 
Thank you very much, I have a better understanding of code. Yeah I am, except I'm more of an expert in WordPress, just started learning Ruby on Rails
 
yes you can also delete all user_id:nil record and remove condition
great
i am rails and js guy but i do have little bit knowledge of php
how you jump into rails?
 
3:21 AM
Thats awesome, thanks a lot. Helped me understand it better, huge learning curve learnt.
Yeah I'm trying to move away from PHP and into Ruby on Rails for obvious reasons, its a lot more elegant and cleaner
 
yeah it is more elegant you will enjoy coding and you feel productive in rails
are you using skype?
 
Are you a freelancer?
 
yeah
but i am doing it in part time
 
Yeah same, I'm just restarting my freelance business
Im in a completely non coding industry which I've been in for far too long, getting tired of it. So I am planning to start freelance work part time outside of my day job hours
Its hard though, get tired very quickly
 
great in rails you will get more work and more $ also
can i add you on skype?
 
3:27 AM
Yeah only thing is I am thinking it might be smarter to build a pre made skeletal design or two and use them when I start a new web site project for a client
Yeah sure thing, glenswebdesign
 
did you learn ruby?
 
Yup.
 
can i post my answer?
 
Geez can't believe I was silly enough to try looking for an associated record with no actual pointing ID to use
Sure can! :)
 
will you accept it and upvote it
 
3:31 AM
oh whoops, yes of course, sorry been a while since I've been on stack overflow, two secs ill do it now
 
ok
 
Might need to post it as an answer not in the comment section, I think I have up voted the comment
 
i posted answer pleasce check it
thanks mate
 

last day (14 days later) »