Trying to print all instances of the company and product in a join table
but unable to get the name : actually just trying to print every
instance of a company in the join table
allocation.rb
class Allocation < ActiveRecord::Base
belongs_to :company
belongs_to :product
end
company.rb
class Company < ActiveRecord::Base
has_many :products
has_many :allocations
has_many :products, :through => :allocations
end
product.rb
class Product < ActiveRecord::Base
belongs_to :company
has_many :allocations
has_many :companies, :through => :allocations
end
Controller:
class AllocationsController < ApplicationController
def show
@company = Company.find(params[:id])
@allocations = @company.allocations.all
end
def index
@allocations = Allocation.all
end
def new
@allocations = Allocation.new
end
def create
@allocations = Allocations.new(params[:allocations])
if @allocations.save
flash[:notice] = "Successfully created allocations."
redirect_to @allocations
else
render :action => ''new''
end
end
The index html page
<% title "Allocations" %>
<table>
<tr>
<th>Company</th>
<th>Product</th>
<th>Quantity</th>
</tr>
<% for alloc in @allocations %>
<tr>
<td><%=h alloc.company_id %></td>
<td><%=h alloc.product_id %></td>
<td><%=h alloc.quantity %></td>
</tr>
<% end %>
</table>
Is there a way to loop through each row, print the company name and the
product in the join table?
I have tried this but get error: no method allocations defined
<% title "Allocations" %>
<% @company.allocations.each_with_index do |allocation, index| %>
<%= allocation.product.name %>
...
<% end %>
--
Posted via http://www.ruby-forum.com/.
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
On 23 January 2011 22:42, Craig Leppan <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Trying to print all instances of the company and product in a join table > but unable to get the name : actually just trying to print every > instance of a company in the join table > > allocation.rb > class Allocation < ActiveRecord::Base > belongs_to :company > belongs_to :product > end > > company.rb > class Company < ActiveRecord::Base > has_many :products > has_many :allocations > has_many :products, :through => :allocationsyou cannot have the same association twice, make the second one something like has_many :allocated_products, :through => allocations then for Allocation belongs_to :allocated_product, :class_name => ''Product'' and the table needs an allocated_product_id column. I think that is right, though I have never done this with :through association. So you have company.products and company.allocated_products. Colin -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Colin
Thanks, this worked like a charm, brilliant!
I can now do everything but create the allocations and formtastic is not
sending the action create in the form to the controller
My Controller Allocations:
class AllocationsController < ApplicationController
def show
@company = Company.find(params[:id])
@allocations = @company.allocations.all
end
def index
@allocations = Allocation.all
end
def new
@company = Company.find(params[:id])
@allocations = Allocation.new
end
def create
@allocations = Allocations.new(params[:allocations])
if @allocations.save
flash[:notice] = "Successfully created allocations."
redirect_to @allocations
else
render :action => ''new''
end
end
The view is:
<% title "New Allocation" %><%= @company.name %>
<% semantic_form_for @allocations do |f| %>
<% f.inputs do %>
<%= f.input :allocated_product %>
<%= f.input :quantity %>
<% end %>
<%= f.buttons %>
<% end %>
I have also customized routes:
map.allocations ''companies/:id/allocations'', :controller
=>
"allocations", :action => "show"
map.clients ''clients'', :controller =>
"allocations"
map.allocate ''companies/:id/allocations/new'', :controller
=>
"allocations", :action => "new"
The output on the console is strange as I don''t get the
"create" action
called just "show" and see in show action nothing has been added.
Processing AllocationsController#show (for 127.0.0.1 at 2011-01-24
21:17:41) [POST]
Parameters: {"commit"=>"Create Allocation",
"action"=>"show",
"authenticity_token"=>"zpScMwZRZix3Ube1aTbQJplv+ZI0KXpskD7xB4yOo3g=",
"id"=>"1",
"allocation"=>{"quantity"=>"90",
"allocated_product_id"=>"2"},
"controller"=>"allocations"}
User Columns (1.1ms) SHOW FIELDS FROM `users`
Company Columns (0.7ms) SHOW FIELDS FROM `companies`
Company Load (0.1ms) SELECT * FROM `companies` WHERE
(`companies`.`id` = 1)
Allocation Load (0.1ms) SELECT * FROM `allocations` WHERE
(`allocations`.company_id = 1)
Rendering template within layouts/welcome
Rendering allocations/show
Allocation Columns (0.6ms) SHOW FIELDS FROM `allocations`
CACHE (0.0ms) SELECT * FROM `companies` WHERE (`companies`.`id` = 1)
Product Columns (0.7ms) SHOW FIELDS FROM `products`
Product Load (0.1ms) SELECT * FROM `products` WHERE (`products`.`id`
= 1)
Completed in 123ms (View: 21, DB: 4) | 200 OK
[http://0.0.0.0/companies/1/allocations]
--
Posted via http://www.ruby-forum.com/.
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
On 24 January 2011 19:22, Craig Leppan <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Colin > > Thanks, this worked like a charm, brilliant! > I can now do everything but create the allocations and formtastic is not > sending the action create in the form to the controller > > My Controller Allocations: > class AllocationsController < ApplicationController > > def show > @company = Company.find(params[:id]) > @allocations = @company.allocations.all > end > > def index > @allocations = Allocation.all > end > > def new > @company = Company.find(params[:id]) > @allocations = Allocation.new > end > > def create > @allocations = Allocations.new(params[:allocations]) > if @allocations.save > flash[:notice] = "Successfully created allocations." > redirect_to @allocations > else > render :action => ''new'' > end > end > > The view is: > <% title "New Allocation" %><%= @company.name %> > <% semantic_form_for @allocations do |f| %> > <% f.inputs do %> > <%= f.input :allocated_product %> > <%= f.input :quantity %> > <% end %> > <%= f.buttons %> > <% end %> > > I have also customized routes: > map.allocations ''companies/:id/allocations'', :controller => > "allocations", :action => "show" > map.clients ''clients'', :controller => "allocations" > map.allocate ''companies/:id/allocations/new'', :controller => > "allocations", :action => "new" > > The output on the console is strange as I don''t get the "create" action > called just "show" and see in show action nothing has been added. > > Processing AllocationsController#show (for 127.0.0.1 at 2011-01-24 > 21:17:41) [POST] > Parameters: {"commit"=>"Create Allocation", "action"=>"show", > "authenticity_token"=>"zpScMwZRZix3Ube1aTbQJplv+ZI0KXpskD7xB4yOo3g=", > "id"=>"1", "allocation"=>{"quantity"=>"90", > "allocated_product_id"=>"2"}, "controller"=>"allocations"} > User Columns (1.1ms) SHOW FIELDS FROM `users` > Company Columns (0.7ms) SHOW FIELDS FROM `companies` > Company Load (0.1ms) SELECT * FROM `companies` WHERE > (`companies`.`id` = 1) > Allocation Load (0.1ms) SELECT * FROM `allocations` WHERE > (`allocations`.company_id = 1) > Rendering template within layouts/welcome > Rendering allocations/show > Allocation Columns (0.6ms) SHOW FIELDS FROM `allocations` > CACHE (0.0ms) SELECT * FROM `companies` WHERE (`companies`.`id` = 1) > Product Columns (0.7ms) SHOW FIELDS FROM `products` > Product Load (0.1ms) SELECT * FROM `products` WHERE (`products`.`id` > = 1) > Completed in 123ms (View: 21, DB: 4) | 200 OK > [http://0.0.0.0/companies/1/allocations]Sorry, I am not sure what question you are asking. Please explain simply what you are doing and what is or is not happening. Try not to provide more code than necessary. It cannot be necessary to show us four controller actions for example. Colin -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Sorry: will try again
I have now managed to do the modeling using the suggestions above
class Allocation < ActiveRecord::Base
belongs_to :company
belongs_to :allocated_product, :class_name => ''Product''
end
class Company < ActiveRecord::Base
has_many :products
has_many :allocations
has_many :allocated_products, :through => :allocations
class Product < ActiveRecord::Base
belongs_to :company
has_many :allocations
has_many :companies, :through => :allocations
I am trying to create a new company and at the same time allocate a
product as allocated product with quantity to allocate. The product
table has a company_id as it belongs to a company (supplier), but here
the allocated products needs to be allocated to the companies to allow
them to be clients and hence the join table: allocations
class CompaniesController < ApplicationController
def new
@allocations = Allocation.all
@company = Company.new
end
def create
@company = Company.new(params[:company])
params[:allocated_products].each_value do |k|
@company.allocated_products.build(k)
end
if @company.save
flash[:notice] = "Successfully created company."
redirect_to @company
else
render :action => ''new''
end
end
The view:
<% title "New Company" %>
<% semantic_form_for @company do |f| %>
<% f.inputs do %>
<%= f.input :name %>
<%= f.input :contacts, :as => :check_boxes, :label =>
"Contacts
representing the company" %>
<%= f.input :allocated_products %>
<% end %>
<%= f.buttons %>
<% end %>
<p><%= link_to "Back to List", companies_path %></p>
The form is properly displayed but when saving errors with:
NoMethodError in CompaniesController#create
undefined method `each_value'' for nil:NilClass
I assume this means it cannot identify the array that is coming: should
I be able to see the id of params in the html source?
If I did this manually and not using Formtastic what would it look like:
I have looked at fields_for but then cannot put it into a collection
select method.
--
Posted via http://www.ruby-forum.com/.
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.