Last active
August 29, 2015 14:24
-
-
Save ysinc88/04777b4bff5da1530819 to your computer and use it in GitHub Desktop.
Trying to understand join tables in Rails
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
NameError in LampsController#index | |
uninitialized constant Lamp::Constituents | |
Extracted source (around line #158): | |
156 | |
157 | |
158 | |
159 | |
160 | |
161 | |
end | |
raise NameError.new("uninitialized constant #{candidates.first}", candidates.first) | |
end | |
end | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<p id="notice"><%= notice %></p> | |
<h1>Listing Lamps</h1> | |
<table class="table table-bordered table-condensed"> | |
<thead> | |
<tr> | |
<th>Model</th> | |
<th>Description</th> | |
<th>Sell</th> | |
<th>Cost</th> | |
<th>Stock</th> | |
<th>Watt</th> | |
<th>Width</th> | |
<th>Length</th> | |
<th>Fan</th> | |
<th>LEDs/th> | |
<th>PSU</th> | |
<th>Led sum4</th> | |
<th colspan="3"></th> | |
</tr> | |
</thead> | |
<tbody> | |
<% @lamps.each do |lamp| %> | |
<tr> | |
<td><%= lamp.model %></td> | |
<td><%= lamp.description %></td> | |
<td><%= lamp.sell %></td> | |
<td><%= lamp.cost %></td> | |
<td><%= lamp.stock %></td> | |
<td><%= lamp.watt %></td> | |
<td><%= lamp.width %></td> | |
<td><%= lamp.length %></td> | |
<td> | |
<%= lamp.constituents.each do |c| %> | |
<%= c.name %> | <%= c.count%> | |
<% end %> | |
</td> | |
<td><%= lamp.led_sum2 %></td> | |
<td><%= lamp.led_sum3 %></td> | |
<td><%= lamp.led_sum4 %></td> | |
<td><%= link_to 'Show', lamp %></td> | |
<td><%= link_to 'Edit', edit_lamp_path(lamp) %></td> | |
<td><%= link_to 'Destroy', lamp, method: :delete, data: { confirm: 'Are you sure?' } %></td> | |
</tr> | |
<% end %> | |
</tbody> | |
</table> | |
<br> | |
<%= link_to 'New Lamp', new_lamp_path %> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
:lamp_id, :constituent_id, :sum |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class LampsController < ApplicationController | |
before_action :set_lamp, only: [:show, :edit, :update, :destroy] | |
respond_to :html | |
layout 'admin' | |
def index | |
@lamps = Lamp.all | |
end | |
def show | |
respond_with(@lamp) | |
end | |
def new | |
@lamp = Lamp.new | |
respond_with(@lamp) | |
end | |
def edit | |
end | |
def create | |
@lamp = Lamp.new(lamp_params) | |
@lamp.save | |
respond_with(@lamp) | |
end | |
def update | |
@lamp.update(lamp_params) | |
respond_with(@lamp) | |
end | |
def destroy | |
@lamp.destroy | |
respond_with(@lamp) | |
end | |
private | |
def set_lamp | |
@lamp = Lamp.find(params[:id]) | |
end | |
def lamp_params | |
params.require(:lamp).permit(:model, :description, :sell, :cost, :stock, :watt, :width, :length, :led_sum1, :led_sum2, :led_sum3, :led_sum4) | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Lamp < ActiveRecord::Base | |
has_many :joiners | |
has_many :constituents, through: :joiners | |
end | |
class Constituent < ActiveRecord::Base | |
belongs_to :constituent_type | |
has_many :joiners | |
has_many :lamps, through: :joiners | |
end | |
class Joiner < ActiveRecord::Base | |
belongs_to :constituents | |
belongs_to :lamps | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment