RailsCasts Pro episodes are now free!

Learn more or hide this

Tristan Smith's Profile

GitHub User: darcnite3000

Comments by Tristan Smith

Avatar

I'm placing a comment onto this episode as it seems the most relevant.
Has anyone done a good example on the use of rails has_and_belongs_to_many, as i cannot think of a scenario in which i do not use has_many through: and i just cannot get my head around the habtm function...

Avatar

As a follow up to my previous comment i used the following
in my routes and dropped the resource :sessions

ruby
  get 'signup', to: 'users#new', as: 'signup'
  post 'signup', to: 'users#create', as: 'signup'
  get 'login', to: 'sessions#new', as: 'login'
  post 'login', to: 'sessions#create', as: 'login'
  get 'logout', to: 'sessions#destroy', as: 'logout'

NB: i still have resource :users for the full rest interface there

also with this i had to change the login form to be form_tag login_path
and my signup form to be simple_form_for @user, url: signup_path

Avatar

I just notice something with this, when you pass a failed login or signup back you lose the /login(/signup) and get given /sessions(/users)
any ideas on how to fix this?

Avatar

I have to agree that this cast was timed correctly for me as i was looking into something that could benefit from a polymorphic relation.
the other thing which i found useful is this code from stackoverflow, which shows how to set up a has many through polymorphic table

ruby
class Widget < ActiveRecord::Base
  has_many :widget_groupings

  has_many :people, :through => :widget_groupings, :source => :grouper, :source_type => 'Person'
  has_many :aliens, :through => :widget_groupings, :source => :grouper, :source_type => 'Alien'
end

It looks like a great way to link locales to items for me