← Back to Upcase

Polymorphic Associations

(Upcase ) #1

© 2012 - 2017 thoughtbot, inc. Upcase, the design of a robot, and thoughtbot are registered trademarks of thoughtbot, inc.

This is a companion discussion topic for the original entry at https://thoughtbot.com/upcase/videos/intermediate-rails-3

(Tom Freudenheim) #2

Isn’t the reference to shout.content.body in dashboard/show.html.erb a violation of the law of demeter (and Sandy Mertz’s rules).

I added delegate :body, to: :content to the shouts model and then changed it to shout.body in the dashboard/show.html.erb

(Kadu Ribeiro) #3

Now after this, it fails when trying to save the Shout with invalid length. (it fails the validation from TextShout and pass nil to content). Probably missing some validates_associated

(Alwan Mortada) #4

So I came about this error on create action a shout. It turns out to he the shout/_shout.html.erb
<%= link_to shout.username, shout.user %> in the video but it should actually look like this
<%= link_to shout.username, users_path(shout.user) %>

(Andrew Rodrigues) #6

Can you post your code?

(Andrew Rodrigues) #7

You should post your code. In rails, when you set up routes for a model through resource with the show action, using in a get request will automatically route to the show with the models id passed. So essentially, if you’re routes are set up correctly, users_path(shout.user) is a redundant way of saying just shout.user. There’s an example shown in the following documentation: http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to. You’ll see it in the second example, and it’s the documentation, so if you want to read up on it, there you go! Check it out! :slight_smile:

(Alwan Mortada) #8
Rails.application.routes.draw do

  constraints Clearance::Constraints::SignedIn.new do
root to: "dashboards#show"
  resources :shouts, only: [:create, :show]

  root to: "homes#show"
  resources :passwords, controller: "clearance/passwords", only: [:create, :new]
  resource :session, only: [:create]

  resources :users, only: [:create] do
resource :password,
  controller: "clearance/passwords",
  only: [:create, :edit, :update]

  get "/sign_in" => "sessions#new", as: "sign_in"
  delete "/sign_out" => "sessions#destroy", as: "sign_out"
  get "/sign_up" => "users#new", as: "sign_up"
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html


	<%= link_to shout.username, users_path(shout.user) %> 
	<%= render shout.content %>
	<%= link_to shout do %>
		<%= time_ago_in_words(shout.created_at) %> ago
	<% end %>

(Andrew Rodrigues) #9

The problem is here. You need to make it:

resources :users, only: [:create, :show]

Then you should be able to do

link_to shout.username, shout.user

I hope that helps! :slight_smile:

(Jan Ferko) #10

I went through whole example in lesson and I run into one problem. ShoutsController#create validations for body are not run, which leads into weird state when Shout is saved, but TextShout is not. What is proper way of propagating validations from association in Rails? Do I have to write custom validation method?
Thanks for help.

UPDATE: As most of the times, answer was quite simple I just had to read Rails docs more carefully. :slight_smile:

(B Patel) #11

Would someone help me understand why we are using reversible. I’m not quite understanding the directions of migrations. It’s clear to me when we do an additional migration (which I believe is migrating ‘up’) we are creating a record in TextShout and updating the shouts table. What’s happening when we are migrating down? When does that even occur?

(William Larry) #12

I’m having trouble with carrying over the shouts to a text shouts. My migration was successful but when I checked my database they were not in the text shouts table.