rails - Data insertion within migration don't work - ruby-on-rails-4

I'm trying to insert data with migration and don't understand what's wrong with this code:
def change
create_table :sourcebuster_settings do |t|
t.integer :session_length
t.boolean :use_subdomains, default: false
t.string :main_host
t.timestamps
end
Sourcebuster::Setting.create session_length: 30,
use_subdomains: false
end
create_table works fine, but no data inserted after migration. No errors in console.
Previous migrations with the same method of data insertions works fine also. Problem only with this one.

I got it. In my setting model:
before_save { self.main_host = main_host.gsub(/(http:|https:|www\.|:|\/)/,'').downcase if self.main_host }
validates_presence_of :main_host, if: lambda { self.use_subdomains }
validates :main_host, format: { with: VALID_HOST_REGEX }, allow_blank: true
Added allow_blank: true to main_host validation.

Related

Rails 4 has_many through for many to many association fails when creating association

Here is my schema:
create_table :policies do |t|
t.string :name
t.timestamps
end
create_table :permissions do |t|
t.string :name
t.string :method
t.string :controller
t.timestamps
end
create_join_table :policies, :permissions do |t|
t.index :policy_id
t.index :permission_id
end
And here is the code I am using to create the records and their associations:
policy = Policy.create! name: "View All"
permission = Permission.create!({
name: "View Events",
method: "index",
controller: "Events"
})
policy.permissions << permission
And its returning the following error:
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: relation "permission_policies" does not exist
The table name that was created through the migration is policies_permissions
I wonder if this is an issue with the class names not being inferred properly
Swap the positions of permissions and policies in the migration so that you create permissions_policies instead of policies_permissions. Rails infers the class names for a join table in alphabetical order.
See this answer for more information on join table naming conventions.

Accessing database info in rails controller

I'm having trouble defining an amount for a pay action in my item controller. I'm trying to pull in the item price stored in the db. I've tried everything. I reread this, http://guides.rubyonrails.org/action_controller_overview.html, and still no luck.
In items controller:
def pay
# Find the user to pay.
user = User.find( params[:id] )
amount = #I have no idea what to put here
fee = 100
If I put in a number it works fine. However when I try to access the item price that is stored in the db, that is when things blow up.
I've tried various definitions:
#item.price #fail
item_price #fail
Item.find (params: [:price]) #fail
Item.price #fail
#EVERYTHING FAILS
Schema:
ActiveRecord::Schema.define(version: 20150127171203) do
create_table "items", force: true do |t|
t.string "name"
t.decimal "price"
t.text "description"
t.integer "booth_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "category_id"
t.integer "user_id"
t.string "image"
t.string "image_two"
end
create_table "users", force: true do |t|
t.string "name"
t.string "email"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "password_digest"
t.string "remember_digest"
t.string "activation_digest"
t.boolean "activated", default: false
t.datetime "activated_at"
t.string "reset_digest"
t.datetime "reset_sent_at"
t.boolean "admin", default: false
t.string "avatar"
t.text "about"
t.string "location"
t.string "publishable_key"
t.string "secret_key"
t.string "stripe_user_id"
t.string "currency"
end
(edit) I also tried defining items in the pay action and that did not help either. 'item = Item.find ( params[:id]'
Does anyone know the right way to do this? I feel like this should be easy but I'm having trouble with it.
Thanks for your consideration and help!
How is a user related to an item? I ask this because if you are using active record, the associations you make in your database will determine the methods that are created for you. Does a user has_many items in your model? Does an item belong_to a user? In which case would be
#user = User.find(1)
and you could find the price like
#item = #user.items.find(1)
#price = #item.price
EDIT
Ok, I took a look at the code you provided here. This example shows a pay method defining a route in the users controller which seems to only be setup to accept a one time fee. In the comments above it, it even says make a one-off payment so we know this is true.
Even in this example here they explicitly say it's for a fixed amount. Now how do we make it dynamic? Well, look at the code. There's some logic in the pay controller isn't there?
You see that Stripe::Charge object being created?
charge = Stripe::Charge.create(
{
amount: amount,
currency: user.currency,
card: params[:token],
description: "Test Charge via Stripe Connect",
application_fee: fee
},
# Use the user-to-be-paid's access token
# to make the charge.
user.secret_key
)
You can move this logic into your item MODEL. Call it pay or something as long as it takes in the user as a parameter.
def pay(user)
amount = self.price
begin
charge = Stripe::Charge.create(
{
amount: amount,
currency: user.currency,
card: user.token,
description: "Test Charge via Stripe Connect",
application_fee: fee
},
# Use the user-to-be-paid's access token
# to make the charge.
user.secret_key
)
flash[:notice] = "Charged successfully! <a target='_blank' rel='connected-account' href='https://dashboard.stripe.com/test/payments/#{charge.id}'>View in dashboard ยป</a>"
rescue Stripe::CardError => e
error = e.json_body[:error][:message]
flash[:error] = "Charge failed! #{error}"
end
end
Then somewhere in your items CONTROLLER define the pay method instead of users like you had before(Make sure you change your routes accordingly). Then call the pay method you just defined, this way you have the price and you can find the user that is currently logged in.
def pay
user = User.find(session[:user_id])
item = Item.find(params[:id])
item.pay(user)
redirect "somewhere"
end

ActiveRecord::StatementInvalid: PG::DatatypeMismatch: ERROR: column is of type integer[] but default expression is of type integer Rails 4.1.1

I am using Rails 4.1.1 and pg (0.17.1) gem I having error while running the migration
ActiveRecord::StatementInvalid: PG::DatatypeMismatch: ERROR: column "page_ids" is of type integer[] but default expression is of type integer
here is my migration code
class CreatePages < ActiveRecord::Migration
def change
create_table :pages do |t|
t.string :name
t.integer :page_ids, array: true, null: false, default: '{}'
t.timestamps
end
end
end
the array: true not working
Try:
t.integer :page_ids, array: true, null: false, default: []

Ember insert record with multiple relationships - not working

I've been stuck on this problem for two days now, and have not been able to find a solution online. I am hoping some of you might be able to help.
I've got the following models:
App.Project = DS.Model.extend({
name: DS.attr('string'),
project_teams: DS.hasMany('projectTeam')
});
App.ProjectTeam = DS.Model.extend({
sde_effort: DS.attr('number'),
project: DS.belongsTo('project'),
team: DS.belongsTo('team')
});
App.Team = DS.Model.extend({
project_teams: DS.hasMany('projectTeam'),
name: DS.attr('string')
});
And the following code in my projectsController:
var project = store.createRecord('project', {
name: projectName
});
project.save().then(function(projectRecord){
var teamHookerUpper = function(teamId, sdeEffort, idx){
store.find('team', teamId).then(function(team){
var project_team = store.createRecord('project_team', {
project: projectRecord,
sde_effort: sdeEffort,
team: team
});
console.log('project team has been created');
project_team.save().then(function(pt){
projectTeamIds.push(pt.get('id'));
if(idx == sdeHeads.length - 1){
console.log(projectRecord);
projectRecord.set('project_teams', projectTeamIds);
projectRecord.save();
}
});
});
}
for(var i = 0; i < sdeHeads.length; i++){
teamHookerUpper(teamIds[i], sdeHeads[i], i);
}
});
The logic is the following:
Get the value from a txt in the front end and create a new Project. When you create the project, select all teamIds and sdeEfforts (a metadata value) associated with that project, create multiple TeamProject objects and link them to the newly created Project. The Team value associated with ProjectTeam is just an id and I am able to retrieve the object by making a call to the DB.
At present the Project object is created successfully, and multiple ProjectTeam objects are created successfully, however when adding the ProjectTeams, the values for project and team are null. Furthermore, you see that each ProjectTeam object i create a push to an array. Once i've iterated over all teamIds (from the front end), i add the array of ProjectTeams to Project and update Project.
However, when inspecting the ajax call, I see that a PUT request is sent to the appropriate url however, no data is sent whatsoever (status code 204)
I have been stuck on this for two days and have exhausted all the resources I managed to find. I am hoping the community will be able to assist me with this one.
Is there another approach of going about this? Have I been fixated into this approach that I haven't considered other approaches? Please give me some guidance, that would be highly appreciated.
EDIT:-----
here are ajax calls and their respective responses:
{"project":{"name":"fghfg"}} <- post Project
{"project_teams":[],"project":{"id":3,"name":"fghfg", "created_at":"2014-01-03T12:11:28.372Z","updated_at":"2014-01-03T12:11:28.372Z","project_teams":[]}} <- response
{"project_team":{"sde_effort":34,"project":"3","team":"1"}} <- post ProjectTeam
{"projects":[],"teams":[],"project_team":{"id":2,"sde_effort":34,"created_at":"2014-01-03T12:11:28.494Z","updated_at":"2014-01-03T12:11:28.494Z","project":null,"team":null}} <- response
{"project_teams":[],"team":{"id":1,"name":"Team 1","created_at":"2014-01-03T12:10:42.787Z","updated_at":"2014-01-03T12:10:42.787Z","project_teams":[]}} <- response for GET teams/1
{"project":{"name":"fghfg"}} <- PUT project. No response for this call :/
EDIT 2:
Here's the corresponding data schema in ruby
ActiveRecord::Schema.define(version: 20131230144053) do
create_table "project_teams", force: true do |t|
t.integer "sde_effort"
t.integer "project_id"
t.integer "team_id"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "project_teams", ["project_id"], name: "index_project_teams_on_project_id"
add_index "project_teams", ["team_id"], name: "index_project_teams_on_team_id"
create_table "projects", force: true do |t|
t.string "name", null: false
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "teams", force: true do |t|
t.string "name", null: false
t.datetime "created_at"
t.datetime "updated_at"
end
end

RubyMine Rails console create error

I'm currently working on a Ruby on Rails application using RubyMine 5.4.3.2.1. I'm using Rails 4 and Ruby 1.9.3p429. In my application, I have a class file 'user.rb' with the following code:
class User < ActiveRecord::Base
validates :first_name, presence: true
validates :last_name, presence: true
VALID_EMAIL_REGEX = /\A[\w+\-.]+#[a-z\d\-]+(\.[a-z]+)*\.[a-z]+\z/i
validates :email, presence: true, format: { with: VALID_EMAIL_REGEX }, uniqueness:{case_sensitive: false }
validates :password, length: { minimum: 6}
has_secure_password
before_save { self.email = email.downcase }
end
and a related migration file '[timestamp]_create_users.rb' with the following:
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :first_name
t.string :last_name
t.string :email
t.string :password_digest
t.timestamps
end
end
end
Using the RubyMines Run Rake Task, I ran a db:migration to create the users table. The problem is that RubyMines doesn't accept any User.create command to enter data into the database. E.g
User.create (first_name:"John",last_name:"Doe",email:"jdoe#example.com",password:"testing",password_confirmation:"testing")
The error it gives is
SyntaxError: (irb):1: syntax error, unexpected tLABEL
User.create (first_name:"John",last_name:"Doe",ema...
^
(irb):1: syntax error, unexpected ',', expecting $end
...er.create (first_name:"John",last_name:"Doe",email:"jdoe#...
^
This works fine when I run it in my command prompt using the 'rails console', but it gets tedious to repeatedly refer to the command prompt. I've tried running the Rails Console in RubyMine in both default and development, with neither yielding a positive result. Can anyone tell me what I'm doing wrong and how to resolve this?
remove the space between User.create and the left parenthesis User.create(...) instead of User.create (...)
Cheers