Skip to content

Instantly share code, notes, and snippets.

@px-amaac
Last active August 29, 2015 14:11
Show Gist options
  • Save px-amaac/67f3428fc828b9ab4ce2 to your computer and use it in GitHub Desktop.
Save px-amaac/67f3428fc828b9ab4ce2 to your computer and use it in GitHub Desktop.
class Codeblock < ActiveRecord::Base
has_many :sections, as: :item
end
class CodeblocksController < ApplicationController
before_action :correct_step, only: [:create, :destroy]
def create
@codeblock = @step.codeblocks.create(codeblock_params)
if @codeblock.save
@section = @step.sections.build
@section.item = @codeblock
@section.save
flash[:success] = "CodeBlock added"
else
flash[:error] = "Failed to add CodeBlock"
end
redirect_to tutorial_step_url(@tutorial, @step)
end
# DELETE /tutorials/1
# DELETE /tutorials/1.json
def destroy
@codeblock = @step.codeblocks.find_by(id: params[:id])
@codeblock.destroy
respond_to do |format|
format.html { redirect_to tutorial_step_url(@tutorial, @step), notice: 'CodeBlock was successfully destroyed.' }
format.json { head :no_content }
end
end
private
def correct_step
@tutorial = current_user.tutorials.find_by(id: params[:tutorial_id])
@step = @tutorial.steps.find_by(id: params[:step_id])
redirect_to root_url if @step.nil? || @tutorial.nil?
end
def codeblock_params
params.require(:codeblock).permit(:code, :description)
end
end
[45] pry(main)> @section = @step.sections.build
=> #<Section id: nil, step_id: 6, item_id: nil, item_type: nil, created_at: nil, updated_at: nil>
[46] pry(main)> @codeblock = Codeblock.build
NoMethodError: undefined method `build' for #<Class:0x00000005b4fac8>
from /home/vagrant/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.2/lib/active_record/dynamic_matchers.rb:26:in `method_missing'
[47] pry(main)> @codeblock = Codeblock.new
=> #<Codeblock id: nil, code: nil, description: nil, created_at: nil, updated_at: nil, step_id: nil>
[48] pry(main)> @codeblock.code = 'def end'
=> "def end"
[49] pry(main)> @codeblock.description = 'some text'
=> "some text"
[50] pry(main)> @codeblock.save
(2.9ms) begin transaction
SQL (5.6ms) INSERT INTO "codeblocks" ("code", "created_at", "description", "updated_at") VALUES (?, ?, ?, ?) [["code", "def en
d"], ["created_at", "2014-12-11 07:37:09.296127"], ["description", "some text"], ["updated_at", "2014-12-11 07:37:09.296127"]]
(5.4ms) commit transaction
=> true
[51] pry(main)> @section.item = @codeblock
=> #<Codeblock id: 15, code: "def end", description: "some text", created_at: "2014-12-11 07:37:09", updated_at: "2014-12-11 07:37
:09", step_id: nil>
[52] pry(main)> @section.save
(0.8ms) begin transaction
SQL (4.4ms) INSERT INTO "sections" ("created_at", "item_id", "item_type", "step_id", "updated_at") VALUES (?, ?, ?, ?, ?) [["c
reated_at", "2014-12-11 07:37:34.386648"], ["item_id", 15], ["item_type", "Codeblock"], ["step_id", 6], ["updated_at", "2014-12-11
07:37:34.386648"]]
(5.8ms) commit transaction
=> true
[53] pry(main)> @section.item
=> #<Codeblock id: 15, code: "def end", description: "some text", created_at: "2014-12-11 07:37:09", updated_at: "2014-12-11 07:37
:09", step_id: nil>
[54] pry(main)>
class Section < ActiveRecord::Base
belongs_to :step
belongs_to :item, polymorphic: true
end
I have a section that has a polymorphic association with codeblock
@section = Section.first
it was created by
@codeblock = Codeblock.create(codeblock_params)
if @codeblock.save
@section = @step.sections.build
@section.item = @codeblock
@section.save
end
Here is the section.
@section => #<Section id: 1, step_id: 6, item_id: 7, item_type: "Codeblock", created_at: "2014-12-11 05:09:26", updated_at: "2014-12-11 05:09:26">
I thought that if i then called
@section.item I would get a codeblock from rails doing something like
Codeblock Load (4.1ms) SELECT "codeblocks".* FROM "codeblocks" WHERE "codeblocks"."id" = ? LIMIT 1 [["id", 7]]
Instead I get
[33] pry(main)> @section.item
=> nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment