NULL values in database on create action - ruby-on-rails-3

When i clicked to submit, the parameters is shown with all the data, however, when i save it, it shows as:
SQL (1.0ms) INSERT INTO `billings` (`birth`, `city`, `country`, `created_at`, `gender`, `ictype`, `idno`, `marital`, `name`, `national`, `phone`, `postcode`, `race`, `reference_id`, `shipping_address`, `state`, `street1`, `title`, `updated_at`, `uptime`, `user_id`) VALUES (NULL, NULL, NULL, '2014-08-28 02:55:19', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL, '2014-08-28 02:55:19', NULL, 1)
class Billing < ActiveRecord::Base
# attr_accessible :user_id
attr_accessible :title, :birth, :ictype, :idno, :race, :gender, :national, :street1, :postcode, :city, :state, :marital, :shipping_address
belongs_to :user
end
Class ShoppingCartsController < ApplicationController
def create
#billing = Billing.new(params[:billing])
#billing = current_user.build_billing
#billing.save
end
end
Why is it so?

The answer is:
def create
# #billing = Billing.new(params[:billing])
#billing = current_user.build_billing(params[:billing])
#billing.save
end

Related

ActiveRecord Create ignores value

I am doing this
CasPgtiou.create({:pgt_iou => "a", :pgt_id => "b"})
which results in
INSERT INTO `cas_pgtious` (`created_at`, `pgt_id`, `pgt_iou`, `updated_at`) VALUES ('2015-06-25 02:22:55', NULL, NULL, '2015-06-25 02:22:55')
Mysql2::Error: Column 'pgt_id' cannot be null: INSERT INTO `cas_pgtious` (`created_at`, `pgt_id`, `pgt_iou`, `updated_at`) VALUES ('2015-06-25 02:22:55', NULL, NULL, '2015-06-25 02:22:55')
ActiveRecord::StatementInvalid: Mysql2::Error: Column 'pgt_id' cannot be null: INSERT INTO `cas_pgtious` (`created_at`, `pgt_id`, `pgt_iou`, `updated_at`) VALUES ('2015-06-25 02:22:55', NULL, NULL, '2015-06-25 02:22:55')
Even though the value is there the create function is not taking it.
Also the model I have is like this
class CasPgtiou < ActiveRecord::Base
attr_accessible :pgt_iou, :pgt_id
end
Rails version is 3.1.3. I'm not sure why a thing as simple as this would fail.
PS I have tried create this way also. But same error
CasPgtiou.create(:pgt_iou => "a", :pgt_id => "b")
UPDATE
This thing strangely works
pgtiou = CasPgtiou.new
pgtiou[:pgt_iou] = pgt_iou
pgtiou[:pgt_id] = pgt
pgtiou.save!

CakePHP 2.0 Model relationships (Groups and Users)

So, In my project there are groups and users. Each users belongs to a group, and one of those users is also an Admin of the group. I have been struggling to figure out how to map those relationships.
Currently with the set up I am creating a form that will create a new group, and its admin at the same time (so also a new user).
I want to know how to set this up so A- I can save the user and group correctly and B- So in the form both the user and the group information can be validated and show the errors.
This is the current layout of my DB tables
Groups Table:
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`user_id` int(10) unsigned DEFAULT NULL,
`name` varchar(60) NOT NULL,
`account_active` tinyint(1) DEFAULT NULL,
`create_date` date NOT NULL,
`last_modified` date NOT NULL,
`delete_date` date DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `groups_fk_admin` (`administrator`),
CONSTRAINT `groups_fk_admin` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)
And the Users Table:
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`group_id` int(10) unsigned DEFAULT NULL,
`email` varchar(200) NOT NULL,
`password` varchar(40) NOT NULL,
`first_name` varchar(45) NOT NULL,
`last_name` varchar(45) NOT NULL,
`create_date` date NOT NULL,
`last_modified` date NOT NULL,
`delete_date` date DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `email` (`email`),
KEY `users_group_id_fk` (`group_id`),
CONSTRAINT `users_group_id_fk` FOREIGN KEY (`group_id`) REFERENCES `groups` (`id`)
Then my models are currently setup like....
Group Model:
public $hasMany = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'group_id',
'dependent' => false,
)
);
public $belongsTo = array(
'Administrator' => array(
'className' => 'User',
'foreignKey' => 'user_id',
)
);
Users Model:
public $hasMany = array(
'Admin' => array(
'className' => 'Group',
'foreignKey' => 'user_id',
'dependent' => false,
),
);
public $belongsTo = array(
'Group' => array(
'className' => 'Group',
'foreignKey' => 'group_id',
)
);
Thank you for your help! And let me know if I can provide any more information.
Group hasMany User
User belongsTo Group
And just add another column to the Users table called is_admin or whatever, with the value of the ID of the group it is admin of.
For certain actions you should simply check if it passes a certain condition. So if you want to check if a user is an administrator of group 4, it has to check if it's is_admin id is equal to 4. You could set to 0 for 'normal' users.
Being a property of a user, administrator shouldn't be declared in your DB relation with a User hasMany admin and a group belongsTo admin, admin isn't a model on its own. It is a property of User, a role.

How to do this sql statement using Rails

I have two models as follows:
icon.rb
belongs_to :category
attr_accessible :name, :url, :category_id, :icon_for
# == Schema Information
#
# Table name: icons
# id :integer not null, primary key
# name :string(255)
# url :string(255)
# category_id :integer
# icon_for :string(255)
category.rb
has_many :icons
attr_accessible :name, :adult
end
# == Schema Information
#
# Table name: categories
#
# id :integer not null, primary key
# name :string(255)
In the Icons controller
def index
#icons = Icon.where(:icon_for => params[:icon_for])
#category_names_for_icons = ???????
end
I want to get all the category names for the categories the selected icons are for.
category_names = Category.where(:id => #icons.category_id) how to make this a range?
Am I on the right track?
You want to extract the category_id from each of your #icons into an array:
category_names = Category.where(:id => #icons.map(&:category_id))
The Ruby map function iterates over an array, and returns an array. You can think of the &:category_id bit as calling the category_id function on each item in the array.

the column 'id' is not the primary_key ,how to reset the Relationship

I have a table form the second database,it is not through a migration
class Dzhfeed < Dzxdb
set_table_name "pre_home_feed"
set_primary_key :feedid
end
the table:
CREATE TABLE pre_home_feed (
feedid int(10) unsigned NOT NULL auto_increment ,
appid smallint(6) unsigned NOT NULL default '0' ,
icon varchar(30) NOT NULL default '' ',
uid mediumint(8) unsigned NOT NULL default '0' ,
username varchar(15) NOT NULL default '' ',
dateline int(10) unsigned NOT NULL default '0' ,
friend tinyint(1) NOT NULL default '0',
hash_template varchar(32) NOT NULL default '' ,
hash_data varchar(32) NOT NULL default '' ,
title_template text NOT NULL,
title_data text NOT NULL,
body_template text NOT NULL ,
body_data text NOT NULL,
body_general text NOT NULL ,
image_1 varchar(255) NOT NULL default '' ,
image_1_link varchar(255) NOT NULL default '',
image_2 varchar(255) NOT NULL default '' ,
image_2_link varchar(255) NOT NULL default '' ,
image_3 varchar(255) NOT NULL default '' ,
image_3_link varchar(255) NOT NULL default '' ,
image_4 varchar(255) NOT NULL default '' ,
image_4_link varchar(255) NOT NULL default '' ,
target_ids text NOT NULL ,
id mediumint(8) unsigned NOT NULL default '0' ,
idtype varchar(15) NOT NULL default '' ,
hot mediumint(8) unsigned NOT NULL default '0' ,
PRIMARY KEY (feedid),
KEY uid (uid,dateline),
KEY dateline (dateline),
KEY hot (hot),
KEY id (id,idtype)
) ;
but this table has another column named 'id' and not a primary_key
so ,when i want to create a new Dzhfeed ,I don't know how to set the column named id
my code is
feed = Dzhfeed.new(:appid => 0, :icon => 'doing', :uid => 1, :username => 'admin', :title_template => "xxxxxxxxxx", :body_template => '', :dateline => Time.now, :id => 0)
but it not work
the error is
Mysql::Error: Column 'id' cannot be null: INSERT INTO `pre_home_feed` (`image_3`, `uid`, `id`, `dateline`, `title_template`, `idtype`, `image_1`, `username`, `body_template`, `image_1_link`, `image_3_link`, `friend`, `title_data`, `appid`, `body_data`, `image_2_link`, `hot`, `image_4_link`, `image_4`, `hash_template`, `body_general`, `icon`, `target_ids`, `hash_data`, `image_2`) VALUES ('', 1, NULL, 1304675043, 'xxxxxxxxxx', '', '', 'admin', '', '', '', 0, '', 0, '', '', 0, '', '', '', '', 'doing', '', '', '')
How did you create the table "pre_home_feed". If it is through a migration, it would have added a id column by default and set it as a primary key. In order not to add a 'id' column you have to say :id => false, :force => true in your table definition migration file and re-run the migration. ( Re-running a migration would first require a rollback, rake db:rollback. This command will undo the last migration run. )
The way you're setting the primary key is perfectly fine. You go about creating the objects in the standard way like this;
#feed = Dzhfeed.new( :feedid => :value .. )
I suggest you go through the Rails Guides.
Update:
Linking to a two different databases is a little different. Essentially, you need to use the establish_connection method defined in ActiveRecord::Base to do that:
establish_connection(
:adapter => "postgresql",
:host => "localhost",
:username => "username",
:password => "password",
:database => "database_to_link_to"
)
Update:
According to this post here your problem should be solved using composite_primary_keys gem.
Essentially what you need to do is this:
Install the gem composite_primary_keys, and then
require 'composite_primary_keys'
class Dzhfeed < Dzxdb
set_primary_keys :feedif
end
Notice, it is set_primary_keys and not set_primary_key. This should solve your problem.

Can this query be any more DRY?

I've just checked out the ON DUPLICATE KEY UPDATE for MySQL.
Here is an example query.
$query = 'INSERT INTO `activities`
(`id`,
`hole_id`,
`name_id`,
`start_depth`,
`end_depth`,
`start_time`,
`end_time`
) VALUES (
:id,
:hole_id,
:name_id,
:start_depth,
:end_depth,
:start_time,
:end_time
) ON DUPLICATE KEY UPDATE
`id` = :id,
`hole_id` = :hole_id,
`name_id` = :name_id,
`start_depth` = :start_depth,
`end_depth` = :end_depth,
`start_time` = :start_time,
`end_time` = :end_time
';
There is a lot of repetition there obviously.
Is there a way to say "insert, or if exists use the existing information to update".
I've looked at REPLACE, and it says it inserts and deletes if neccessary. The docs say to insert or update to use the method I've used above.
So can I eliminate doubling up of all that update info?
You can use the VALUES() function to refer to the value of a column rather than repeating the value in the ON DUPLICATE KEY portion. See: http://dev.mysql.com/doc/refman/5.1/en/miscellaneous-functions.html#function_values.
For example:
$query = 'INSERT INTO `activities`
(`id`,
`hole_id`,
`name_id`,
`start_depth`,
`end_depth`,
`start_time`,
`end_time`
) VALUES (
:id,
:hole_id,
:name_id,
:start_depth,
:end_depth,
:start_time,
:end_time
) ON DUPLICATE KEY UPDATE
`id` = VALUES(id),
`hole_id` = VALUES(hole_id),
`name_id` = VALUES(name_id),
`start_depth` = VALUES(start_depth),
`end_depth` = VALUES(end_depth),
`start_time` = VALUES(start_time),
`end_time` = VALUES(end_time)
';