Skip to content

Instantly share code, notes, and snippets.

@colewinans
Last active December 19, 2015 04:09
Show Gist options
  • Save colewinans/5895213 to your computer and use it in GitHub Desktop.
Save colewinans/5895213 to your computer and use it in GitHub Desktop.
null user_id has_many :through
#### User Model ####
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable, :confirmable,
:recoverable, :rememberable, :trackable, :validatable
attr_accessible :email, :password, :password_confirmation,
:remember_me, :username, :profile_image,
:first_name, :last_name
has_many :publications
has_many :posts, :through => :publications
end
#### Posts Model ####
class Post < ActiveRecord::Base
attr_accessible :description, :name, :tag_list, :thumbnail_image, :status, :post_type, :subtitle, :summary
has_one :publication
has_one :user, :through => :publication
end
#### Controller ####
def create
@user = User.find(params[:user_id])
@post = @user.posts.new(params[:post])
@post.build_publication(user_id: @user)
if @post.save
redirect_to post_wizard_path(:id => "step_two", :post_id => @post.id)
else
redirect_to browse_path
end
end
#### View ####
= form_for [@user, @post] do |f|
- if @post.errors.any?
#error_explanation
%h2= "#{pluralize(@post.errors.count, "error")} prohibited this post from being saved:"
%ul
- @post.errors.full_messages.each do |msg|
%li= msg
.field
%span Title:
= f.text_field :name
.actions
= f.submit 'Submit'
#### Log ####
Started POST "/users/3/posts" for 127.0.0.1 at 2013-06-30 08:04:23 -0600
Processing by PostsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"WPg02ogJnTnwu4+GV0daLr/xxpLhGioaCqlIOJIyHZ8=", "post"=>{"post_type"=>"standard"}, "commit"=>"Continue", "user_id"=>"3"}
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = 3 LIMIT 1
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", "3"]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", "3"]]
(0.3ms) BEGIN
SQL (0.6ms) INSERT INTO "posts" ("created_at", "description", "post_image_id", "post_type", "name", "slug", "status", "subtitle", "summary", "thumbnail_image", "updated_at", "user_id") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12) RETURNING "id" [["created_at", Sun, 30 Jun 2013 14:04:23 UTC +00:00], ["description", nil], ["post_image_id", nil], ["post_type", "standard"], ["name", nil], ["slug", nil], ["status", nil], ["subtitle", nil], ["summary", nil], ["thumbnail_image", nil], ["updated_at", Sun, 30 Jun 2013 14:04:23 UTC +00:00], ["user_id", nil]]
SQL (0.6ms) INSERT INTO "publications" ("created_at", "post_id", "updated_at", "user_id") VALUES ($1, $2, $3, $4) RETURNING "id" [["created_at", Sun, 30 Jun 2013 14:04:23 UTC +00:00], ["post_id", 176], ["updated_at", Sun, 30 Jun 2013 14:04:23 UTC +00:00], ["user_id", 3]]
(1.9ms) COMMIT
Publication Load (0.6ms) SELECT "publications".* FROM "publications" WHERE "publications"."post_id" = 176 LIMIT 1
(0.1ms) BEGIN
(0.4ms) UPDATE "publications" SET "post_id" = NULL, "updated_at" = '2013-06-30 14:04:23.710774' WHERE "publications"."id" = 43
(0.3ms) COMMIT
(0.1ms) BEGIN
SQL (0.3ms) INSERT INTO "publications" ("created_at", "post_id", "updated_at", "user_id") VALUES ($1, $2, $3, $4) RETURNING "id" [["created_at", Sun, 30 Jun 2013 14:04:23 UTC +00:00], ["post_id", 176], ["updated_at", Sun, 30 Jun 2013 14:04:23 UTC +00:00], ["user_id", 1]]
(0.4ms) COMMIT
Redirected to http://localhost:3000/posts/176/wizard/step_two
Completed 302 Found in 29ms (ActiveRecord: 7.4ms)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment