{"id":991,"date":"2022-08-30T15:10:34","date_gmt":"2022-08-30T15:10:34","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/frustrated-with-nesting-in-ruby-on-rails-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:10:34","modified_gmt":"2022-08-30T15:10:34","slug":"frustrated-with-nesting-in-ruby-on-rails-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/frustrated-with-nesting-in-ruby-on-rails-collection-of-common-programming-errors\/","title":{"rendered":"Frustrated With Nesting in Ruby on Rails-Collection of common programming errors"},"content":{"rendered":"<p>This is my first post here and to be honest, I don&#8217;t tend to ask many questions, but something has been irritating me for quite some time involving nesting in Ruby on Rails.<\/p>\n<p>I have a <code>class Thought<\/code> (which are members&#8217; individual posts) that are paginated as <code>@share_items<\/code>. I am including a like button that will allow members to like other members&#8217; posts (like Facebook). The like button has the <code>class Opinion<\/code>.<\/p>\n<p>My code thus far:<\/p>\n<pre><code>Class Member\nhas_many :thoughts, dependent: :destroy\nhas_many :opinions, foreign_key: \"fan_id\", dependent: :destroy\nhas_many :fans, through: :opinions\n\nClass Thought\nbelongs_to :member\n\nhas_many :opinions, foreign_key: \"like_id\", dependent: :destroy\nhas_many :likes, through: :opinions\n\nvalidates :member_id, presence: true\n\nClass Opinion\nattr_accessible :like_id\n\nbelongs_to :fan, class_name: \"Member\"\nbelongs_to :like, class_name: \"Thought\"\n\nvalidates :fan_id, presence: true\nvalidates :like_id, presence: true\n\nMember Model\ndef share\nThought.from_members_authenticated(self)\nend\n\nMemberPages Controller\ndef homepage\n@share_items = registered_member.share.paginate(page: params[:page])\nend\n<\/code><\/pre>\n<p>Partial <code>_share.html.erb<\/code>:<\/p>\n<pre><code>\n<\/code><\/pre>\n<ol class=\"member_shares\"><\/ol>\n<pre>\n\n\n<\/pre>\n<p><code>Which renders partial <code>_share_item.html.erb<\/code>:<\/code><\/p>\n<li id=\"%= share_item.id %\"><code>content... ...content<\/code><\/li>\n<pre> \n<\/pre>\n<p><code>Everything goes smoothly until I try to render the form <code>thoughts\/like_form<\/code> inside paginated <code>@share_items<\/code> (<code>class Thought<\/code>). It has bizarre and unexplainable results (to me). Here are the codes leading up to the form.<\/code><\/p>\n<pre><code>Member Model\n\ndef liked?(random_thought)\nopinions.find_by_like_id(random_thought.id)\nend\n\ndef like!(random_thought)\nopinions.create!(like_id: random_thought.id)\nend\n\ndef unlike!(random_thought)\nopinions.find_by_like_id(random_thought.id).destroy\nend\n\nOpinionsController\n\nrespond_to :html, :js\n\ndef create\n@thought = Thought.find(params[:opinion][:like_id])\nregistered_member.like!(@thought)\nrespond_with @thought\nend\n\ndef destroy\n@thought = Opinion.find(params[:id]).like\nregistered_member.unlike!(@thought)\nrespond_with @thought\nend\nend\n<\/code><\/pre>\n<p>And finally the form <code>thoughts\/like_form<\/code>:<\/p>\n<pre><code>\n\n\n\n\n\n\n<\/code><\/pre>\n<p>which will render either <code>thoughts\/unlike<\/code>:<\/p>\n<pre><code>\n\n\n<\/code><\/pre>\n<p>or: <code>thoughts\/like<\/code>:<\/p>\n<pre><code>\n\n\n\n<\/code><\/pre>\n<p>Concluding with <code>opinions\/create.js.erb<\/code>:<\/p>\n<pre><code>$(\"#like_form\").html(\"\")\n$(\"#likes\").html('')\n<\/code><\/pre>\n<p>and <code>opinions\/destroy.js.erb<\/code>:<\/p>\n<pre><code>$(\"#like_form\").html(\"\")\n$(\"#likes\").html('')\n<\/code><\/pre>\n<p>Notice I do something fishy to the js&#8230; I try to tell the like form to target a specific thought (member&#8217;s share). Note: This has been the most successful thus far, but no cigar. It adds fan_id (member) and like_id (thought) to the database and renders the unlike button. But it has unpredicted results. For example: Sometimes, no matter which of the members&#8217; shares I like, it will always render unlike on the first share of the page, although it will add the correct data to the database. Other times it will work correctly, but after refreshing the page, it will either render the like button (even though it was currently liked) or it will render the correct button, but it will raise an error when trying to &#8220;unlike&#8221; the share saying that there is no match to &#8220;destroy \/opinions&#8221; which indicates it is not targeting specific shares.<\/p>\n<p>So to reiterate my my intentional question: How would I &#8220;successfully&#8221; render a like button (with the <code>class Opinion<\/code>) inside a member&#8217;s UNIQUE share (<code>class Thought<\/code>) when it is inside a paginated list defined as <code>@share_items<\/code>.<\/p>\n<p>Sorry if I was confusing, first time posting here and I am new to Ruby. Thank you for any help you may give.<\/p>\n<ol>\n<li>\n<p>I have figured out a way to successfully render the like\/unlike form for a member&#8217;s individual post. While this is uncanny, space consuming and above all: lacks trend; it <em>does<\/em> work.<\/p>\n<p>Instead of rendering <code>like_form.html.erb<\/code>, I added the code directly to <code>share_item.html.erb<\/code> and told the form not to look for <code>@thought<\/code> but rather to look for <code>share_item<\/code> (which is the individual item within the collection of <code>@share_items<\/code> nested inside <code>class Thought<\/code>)<\/p>\n<p><code>thoughts\/unlike.html.erb<\/code> now looks like:<\/p>\n<pre><code>\n\n\n<\/code><\/pre>\n<p>and <code>thoughts\/like.html.erb<\/code><\/p>\n<pre><code>\n\n\n\n<\/code><\/pre>\n<p>and I could omit from the js so it looks like <code>(\"#like_form\")<\/code><\/p>\n<p>As I said, this junks up my partial having to add the code inside of it rather than rendering the form partial, but if I do the latter, I get the error that &#8220;share_item is an unknown or undefined local variable&#8221; While I have accomplished my initial goal getting the form to actually work, I am still seeking suggestions as to how I could render the form rather than adding it directly to my partial, without getting that error message.<\/p>\n<\/li>\n<li>\n<p>When you have nested objects, you should declare your form like this:<\/p>\n<pre><code>form_for [@member, @opinion]\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-09 23:11:45. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>This is my first post here and to be honest, I don&#8217;t tend to ask many questions, but something has been irritating me for quite some time involving nesting in Ruby on Rails. I have a class Thought (which are members&#8217; individual posts) that are paginated as @share_items. I am including a like button that [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-991","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/991","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=991"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/991\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=991"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=991"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=991"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}