view for has_many and belongs_to but with only one associated record

14 views
Skip to first unread message

Mano Svajone

unread,
Jul 19, 2014, 9:01:26 PM7/19/14
to rubyonra...@googlegroups.com
Restaurant Load (1.5ms) SELECT * FROM "restaurants" INNER JOIN
"restaurant_branches" ON "restaurant_branches"."restaurant_id" =
"restaurants"."restaurant_id"
+----------+---------+---------+---------+----------+---------+----------+---------+---------+---------+---------+---------+---------+---------+---------+-------+
| resta... | res_... | res_... | crea... | updat... | user_id | resta... | addr...
| addr... | addr... | addr... | addr... | addr... | numb... | numb... | email |
+----------+---------+---------+---------+----------+---------+----------+---------+---------+---------+---------+---------+---------+---------+---------+-------+
| 27 | DOGG... | WE S... | 2014... | 2014-... | 4 | 28 | 405 ...
| | CHICAGO | IL | 60666 | USA | | | |
| 27 | DOGG... | WE S... | 2014... | 2014-... | 4 | 29 | 111 ...
| | CHICAGO | IL | 60661 | USA | | | |
+----------+---------+---------+---------+----------+---------+----------+---------+---------+---------+---------+---------+---------+---------+---------+-------+

As you can see, I have two records for record 27. This is from a joined
table between restaurants and restaurant_branches. How would I approach
this in a view so that when I select a record on my index.html.erb file
when it gets routed to the show.html.erb file it'll only take one row of
that record and only show me one branch instead of 2?

Thank you for any help.

--
Posted via http://www.ruby-forum.com/.

Sampath Weerasinghe

unread,
Jul 19, 2014, 11:10:40 PM7/19/14
to rubyonra...@googlegroups.com


Which Rails query are you using for getting the output?


--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/91a09235a01d850bda588d149bb1640e%40ruby-forum.com.
For more options, visit https://groups.google.com/d/optout.

Colin Law

unread,
Jul 20, 2014, 3:34:44 AM7/20/14
to rubyonra...@googlegroups.com
Have you checked by using count whether there are in fact two records
returned and this is not just a figment of the logging? If there are
two then also include restaurant_branches.id and I think you will find
there are in fact two branches.

Colin
Reply all
Reply to author
Forward
0 new messages