foreign key not populating in the table on create - rails 3 - ruby-on-rails-3

I have two models, 'product' with 'belongs_to' and 'category' with 'has_many'. Product has a foreign key 'category_id'. And in the form product/_form I'm trying to include category field with a select option which shows the options from the category table. But when I click on submit the category_id doesn't get populated in the product table. Please help...
product_controller.rb
def create
#product = Product.new(params[:product])
respond_to do |format|
if #product.save
format.html { redirect_to([:backend, #product], :notice => 'Product was successfully created.') }
format.xml { render :xml => #product, :status => :created, :location => #product }
else
format.html { render :action => "new" }
format.xml { render :xml => #product.errors, :status => :unprocessable_entity }
end
end
end
_form.html.erb
<%= f.label :category %><br />
<%= f.select :category_id, options_from_collection_for_select(Category.all, :id, :name), :prompt => "Select" %>
models/product.rb
class Product < ActiveRecord::Base
attr_accessible :name, :desc, :price, :is_special
belongs_to :category
end
models/category.rb
class Category < ActiveRecord::Base
attr_accessible :name
has_many :products
end

attr_accessible :name, :desc, :price, :is_special is responsible for this
you should add :category_id to this list.
when you use attr_accessbible, all other field are protected

Related

How get the return value of checkbox in rails

I am sorry because this look like a doubled post, but i saw a lot of other threads and i cant understand anything that i am doing.
I am trying to make an has_and_belongs_to_many but i am stuck.
I managed to make the form display the right information, but i dont know how to save it.
I got:
Orb class:
class Orb < ActiveRecord::Base
attr_accessible :descr, :id, :nome, :orb_type_id, :orbt
validates_presence_of :nome, :orb_type_id
validates :nome, :uniqueness => true
belongs_to :orb_type
has_and_belongs_to_many :books
end
Book class:
class Book < ActiveRecord::Base
attr_accessible :dataf, :datai, :descr, :id, :nome
validates_presence_of :nome
validates :nome, :uniqueness => true
has_and_belongs_to_many :orbs
# allows project page to add items via checkboxes
accepts_nested_attributes_for :orbs
end
A _form:
<% #book.each do |book| %>
<div>
<%= check_box_tag "orb[book_ids][]", book.id, #orb.books.include?(book), id: dom_id(book) %>
<%= book.nome %>
</div>
<% end %>
And the controller:
def new
#book = Book.all
#orb = Orb.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: #orb }
end
end
# GET /orbs/1/edit
def edit
#orb = Orb.find(params[:id])
#book = Book.all
end
# POST /orbs
# POST /orbs.json
def create
#orb = Orb.new(params[:orb])
respond_to do |format|
if #orb.save
format.html { redirect_to #orb, notice: 'save was successful' }
format.json { render json: #orb, status: :created, location: #orb }
else
format.html { render action: "Novo" }
format.json { render json: #orb.errors, status: :unprocessable_entity }
end
end
end
# PUT /orbs/1
# PUT /orbs/1.json
def update
params[:orb][:book_ids] ||= []
#orb = Orb.find(params[:id])
respond_to do |format|
if #orb.update_attributes(params[:orb])
format.html { redirect_to #orb, notice: 'save was successful' }
format.json { head :no_content }
else
format.html { render action: "Editar" }
format.json { render json: #orb.errors, status: :unprocessable_entity }
end
end
end
With this, the form has a checkbox with the right values, but it wont be save anywere.
I dont know what i am doing, can some one me explain what i have to do?
You need a join table to use has_and_belongs_to_many
class CreateTableBooksOrbs < ActiveRecord::Migration
def change
create_table :books_orbs, :id => false do |t|
t.references :orb, :null => false
t.references :book, :null => false
end
add_index :books_orbs, [:book_id, :orb_id], :unique => true
end
end
see here my working version : https://github.com/senayar/books

Rails validation count limit on has_many :through

I've got the following models: Team, Member, Assignment, Role
The Team model has_many Members. Each Member has_many roles through assignments. Role assignments are Captain and Runner. I have also installed devise and CanCan using the Member model.
What I need to do is limit each Team to have a max of 1 captain and 5 runners.
I found this example, and it seemed to work after some customization, but on update ('teams/1/members/4/edit'). It doesn't work on create ('teams/1/members/new'). But my other validation (validates :role_ids, :presence => true
) does work on both update and create. Any help would be appreciated.
Update: I've found this example that would seem to be similar to my problem but I can't seem to make it work for my app.
It seems that the root of the problem lies with how the count (or size) is performed before and during validation.
For Example:
When updating a record...
It checks to see how many runners there are on a team and returns a count. (i.e. 5) Then when I select a role(s) to add to the member it takes the known count from the database (i.e. 5) and adds the proposed changes (i.e. 1), and then runs the validation check. (Team.find(self.team_id).members.runner.count > 5) This works fine because it returns a value of 6 and 6 > 5 so the proposed update fails without saving and an error is given.
But when I try to create a new member on the team...
It checks to see how many runners there are on a team and returns a count. (i.e. 5) Then when I select a role(s) to add to the member it takes the known count from the database (i.e. 5) and then runs the validation check WITHOUT factoring in the proposed changes. This doesn't work because it returns a value of 5 known runner and 5 = 5 so the proposed update passes and the new member and role is saved to the database with no error.
Member Model:
class Member < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
attr_accessible :password, :password_confirmation, :remember_me
attr_accessible :age, :email, :first_name, :last_name, :sex, :shirt_size, :team_id, :assignments_attributes, :role_ids
belongs_to :team
has_many :assignments, :dependent => :destroy
has_many :roles, through: :assignments
accepts_nested_attributes_for :assignments
scope :runner, joins(:roles).where('roles.title = ?', "Runner")
scope :captain, joins(:roles).where('roles.title = ?', "Captain")
validate :validate_runner_count
validate :validate_captain_count
validates :role_ids, :presence => true
def validate_runner_count
if Team.find(self.team_id).members.runner.count > 5
errors.add(:role_id, 'Error - Max runner limit reached')
end
end
def validate_captain_count
if Team.find(self.team_id).members.captain.count > 1
errors.add(:role_id, 'Error - Max captain limit reached')
end
end
def has_role?(role_sym)
roles.any? { |r| r.title.underscore.to_sym == role_sym }
end
end
Member Controller:
class MembersController < ApplicationController
load_and_authorize_resource :team
load_and_authorize_resource :member, :through => :team
before_filter :get_team
before_filter :initialize_check_boxes, :only => [:create, :update]
def get_team
#team = Team.find(params[:team_id])
end
def index
respond_to do |format|
format.html # index.html.erb
format.json { render json: #members }
end
end
def show
respond_to do |format|
format.html # show.html.erb
format.json { render json: #member }
end
end
def new
respond_to do |format|
format.html # new.html.erb
format.json { render json: #member }
end
end
def edit
end
def create
respond_to do |format|
if #member.save
format.html { redirect_to [#team, #member], notice: 'Member was successfully created.' }
format.json { render json: [#team, #member], status: :created, location: [#team, #member] }
else
format.html { render action: "new" }
format.json { render json: #member.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if #member.update_attributes(params[:member])
format.html { redirect_to [#team, #member], notice: 'Member was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: #member.errors, status: :unprocessable_entity }
end
end
end
def destroy
#member.destroy
respond_to do |format|
format.html { redirect_to team_members_url }
format.json { head :no_content }
end
end
# Allow empty checkboxes
# http://railscasts.com/episodes/17-habtm-checkboxes
def initialize_check_boxes
params[:member][:role_ids] ||= []
end
end
_Form Partial
<%= form_for [#team, #member], :html => { :class => 'form-horizontal' } do |f| %>
#...
# testing the count...
<ul>
<li>Captain - <%= Team.find(#member.team_id).members.captain.size %></li>
<li>Runner - <%= Team.find(#member.team_id).members.runner.size %></li>
<li>Driver - <%= Team.find(#member.team_id).members.driver.size %></li>
</ul>
<div class="control-group">
<div class="controls">
<%= f.fields_for :roles do %>
<%= hidden_field_tag "member[role_ids][]", nil %>
<% Role.all.each do |role| %>
<%= check_box_tag "member[role_ids][]", role.id, #member.role_ids.include?(role.id), id: dom_id(role) %>
<%= label_tag dom_id(role), role.title %>
<% end %>
<% end %>
</div>
</div>
#...
<% end %>
Try
class Member < ActiveRecord::Base
...
def validate_runner_count
if self.team.members.runner.count > 5
errors.add(:role_id, 'Error - Max runner limit reached')
end
end
def validate_captain_count
if self.team.members.captain.count > 1
errors.add(:role_id, 'Error - Max captain limit reached')
end
end
end

Update nested attributes before saving to database

Long time reader of Stackoverflow but have never found myself in a position to ask a question (that hasn't already been answered). I guess there's a first time for everything so here it goes...
System Info:
Ruby Version = 1.8.7
Rails Version = 3.2.2
Situation:
We have an application with a user registration system in place. In order to hook up and populate all of our tables correctly, we are utilizing Complex/Nested Forms within the registration view. I actually have the nested forms working perfectly, everything is being populated as it should, its awesome really.
Here is the problem: I need to set one of the value of one of the nested attributes AFTER the form post but BEFORE the records are saved.
Here is a quick example so you can see what I'm talking about a little bit better:
A user registers with our site. When they register a record is created in the Users data table. Each user is also classified as a team_mate (join table) and assigned to their very own individual team (at first). But, a 'team' (table) also has an 'alias' field in it which, on the initial creation of the user we would like to set to the users first name (without having to have them enter their first name into an 'alias' field on the form).
So, I guess the question would be: How to I manually set the value of a nested attribute after the form post and before the records are saved to the database?
A (simplistic) example of the table schema looks is as follows:
Users (id, first_name, last_name, created_at, updated_at)
Team_mates(id, user_id, team_id, created_at, updated_at) - join table
Teams(id, alias, created_at, updated_at)
Models:
User.rb
class User < ActiveRecord::Base
has_many :team_mates, :dependent => :destroy
has_many :teams, :through => :team_mates, :foreign_key => :team_id
accepts_nested_attributes_for :team_mates, :allow_destroy => true
before_save :set_defaults
private
def set_defaults
#want to set :users => :team_mates_attributes => :team_attributes => :alias to #user.first_name here
# Would prefer to handle this here instead of in the controller.
end
end
Team.rb
class Team < ActiveRecord::Base
has_many :team_mates, :dependent => :destroy
has_many :users, :through => :team_mates, :foreign_key => :user_id
end
Team_mate.rb
class TeamMate < ActiveRecord::Base
belongs_to :user
belongs_to :team
accepts_nested_attributes_for :team, :allow_destroy => true
end
Controller
Users_controller.rb
class UsersController < ApplicationController
def new
#user = User.new
#user.emails.build(:is_default_email => 1)
#user.build_login
#user.team_mates.build.build_team(:alias => 'Clinton444', :created_at => Time.new, :updated_at => Time.new)
respond_to do |format|
format.html
format.json { render :json => #match }
end
end
def create
#user = User.new(params[:user])
#user.attributes = ({ "user" => { "team_mates" => { "team" => { "alias" => #user.first_name } } } }) #--this doesn't work...
#user.attributes = ({ :user => { :team_mates => { :team => { :alias => #user.first_name } } } }) #--neither does this...
respond_to do |format|
if #user.save
format.html { redirect_to(#user, :notice => 'User was successfully created.') }
format.json { render :json => #user, :status => :created, :location => #user }
else
format.html { render :action => "new" }
format.json { render :json => #user.errors, :status => :unprocessable_entity }
end
end
end
View
new.html.haml
= form_for(#user, :html => {:class => 'form-horizontal'}) do |f|
- if #user.errors.any?
.alert
%h2
= pluralize(#user.errors.count, "error")
prohibited this post from being saved:
%ul
- #user.errors.full_messages.each do |msg|
%li
= msg
%fieldset
.control-group
= f.label :first_name, :class => "control-label"
.controls
=f.text_field :first_name, :class => "span8"
.control-group
= f.label :last_name, :class => "control-label"
.controls
=f.text_field :last_name, :class => "span8"
= f.fields_for :emails do |e|
=e.hidden_field :is_default_email, :class => "span8"
.control-group
= e.label :email, :class => "control-label"
.controls
=e.text_field :email, :class => "span8"
= f.fields_for :team_mates do |tm|
= tm.fields_for :team do |t|
=t.hidden_field :alias, :class => "span8"
=t.hidden_field :created_at, :class => "span8"
=t.hidden_field :updated_at, :class => "span8"
= f.fields_for :login do |e|
.control-group
= e.label :user_login, :class => "control-label"
.controls
=e.text_field :user_login, :class => "span8"
.control-group
= e.label :password_encrypted, :class => "control-label"
.controls
=e.text_field :password_encrypted, :class => "span8"
.control-group
.controls
=f.submit :class => 'btn btn-primary btn-medium'
And finally
Rails server output on form post
Parameters: {"user"=>{"team_mates_attributes"=>{"0"=>{"team_attributes"=>{"created_at"=>"Wed Jun 06 09:52:19 -0600 2012", "alias"=>"asfs444", "updated_at"=>"Wed Jun 06 09:52:19 -0600 2012"}}}, "first_name"=>"lkjlkjlsdfslkjeowir", "last_name"=>"ouisodifuoixv", "emails_attributes"=>{"0"=>{"is_default_email"=>"1", "email"=>"lpisfsopf#psflsjdk.com"}}, "login_attributes"=>{"user_login"=>"lkjsdfooiusfd", "password_encrypted"=>"[FILTERED]"}}, "utf8"=>"✓", "commit"=>"Create User", "authenticity_token"=>"CQLQ93/0VlncSzMlmtLPHgaVrrvjuHFN+lN6CYCsiR8="}
After looking at the models you might be wondering where emails/logins are coming from. They're built within the model on our system, but are not really part of this question so I omitted the code for them. They are working, so the problem isn't on that side.
Check http://archives.ryandaigle.com/articles/2009/2/1/what-s-new-in-edge-rails-nested-attributes
To support both the creation of new objects and the editing of
existing ones we have to use an array of hashes for one-to-many
associations or a single hash for one-to-one associations. If no :id
property exists then it is assumed to represent a nested model to
create.
Not 100% sure.. I haven't used\tested it before, but this should give you an idea
#user.teams.each do |team|
team.team_mates do |team_mate|
# To edit existing
team_mate.team_attributes = [ { :id => team.id, :alias => #user.first_name } ]
# To create new
team_mate.team_attributes = [ { :alias => #user.first_name } ]
team_mate.save
end
end

Rails: SQL Error

I'm getting a SQL error with this:
Model:
class Comment < ActiveRecord::Base
belongs_to :user
belongs_to :entry
attr_accessible :body
validates :user_id, :presence => true
validates :entry_id, :presence => true
validates :body, :presence => true, :length => {:minimum => 10, :maximum => 5000} #spam/stupid protection
default_scope :order => 'comments.created at sec'
end
Controller
def show
#entry = Entry.find(params[:id])
#comments = #entry.comments.all
...
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => #entry }
end
end
The view is a simple:
<% if #entry.state > 2 %>
<section id="comments">
<% #comments.each do |comment| %>
...loop some stuff...
It looks like you want :order => 'comments.created_at desc', not sec.
And what exactly is the error you are receiving?
I think your error is from here:
default_scope :order => 'comments.created at sec'
Because comments.created at sec you can't have that field in DB

How do I create a join action between a group and a user?

user.rb
has_many :memberships, :dependent => :destroy
has_many :groups, :through => :memberships
membership.rb
class Membership < ActiveRecord::Base
attr_accessible :user_id, :group_id
belongs_to :user
belongs_to :group
validates_uniqueness_of :user_id, :message => "You can only join one group!"
end
group.rb
has_many :memberships, :dependent => :destroy
has_many :users, :through => :memberships
groups_controller.rb
def join
#group = Group.find(params[:id])
#m = #group.memberships.build(:user_id => current_user.id)
respond_to do |format|
if #m.save
format.html { redirect_to(#group, :notice => 'You have joined this group.') }
format.xml { head :ok }
else
format.html { redirect_to(#group, :notice => 'Join error.') }
format.xml { render :xml => #group.errors, :status => :unprocessable_entity }
end
end
end
memberships_controller.rb
class MembershipsController < ApplicationController
before_filter :authenticate_user!
def create
#membership = current_user.memberships.build(:group_id => params[:group_id])
if #membership.save
flash[:notice] = "You have joined this group."
redirect_to :back
else
flash[:error] = "Unable to join."
redirect_to :back
end
end
def destroy
#membership = current_user.memberships.find(params[:id])
#membership.destroy
flash[:notice] = "Removed membership."
redirect_to :back
end
end
Users have groups through a membership model which is a join table. My question. How do I create a join action for users to click on so that they can join a group?
You've already completed step 1, which is adding the controller actions. Now you just need to add the appropriate route and wire it up in your views.
In your routes.rb:
resources :groups do
get 'join', :on => :member
end
In your view file:
<%= link_to "Join this group", join_group_path(#group) %>
That's it!