Ruby on Rails : Tricky Many to Many relationship does not work - activerecord

I am designing a "Lists" mapping feature in my application for transcoding purposes.
So that my mappins_list object points to a source_list object and a target_list object, both being of an instance values_list class.
The design looks classical, but it does not behave as expected:
<td><%= link_to #mappings_list.source_list.name, #mappings_list.source_list%>
raises a
undefined method `source_list' for #<ActiveRecord::Relation::ActiveRecord_Relation_MappingsList:0x00000004217718>
in the index view.
(note that one record was crated with correct attributes, it should be displayed there)
Here is the code:
# == Schema Information
#
# Table name: values_lists
#
# id :integer not null, primary key
# playground_id :integer
# code :string(255)
# name :string(255)
# description :text
# created_by :string(255)
# updated_by :string(255)
# owner_id :integer
# table_name :string(255)
# created_at :datetime
# updated_at :datetime
# software_id :integer
# software_name :string(255)
#
class ValuesList < ActiveRecord::Base
### scope
scope :pgnd, ->(my_pgnd) { where "playground_id=?", my_pgnd }
### before filter
before_create :set_code
### validation
validates :code, length: { maximum: 100 }
validates :name, presence: true, uniqueness: true, length: { maximum: 100 }
validates :description, length: { maximum: 1000 }
validates :created_by , presence: true
validates :updated_by, presence: true
validates :playground_id, presence: true
belongs_to :playground
validates :playground, presence: true # validates that the playground exists
belongs_to :owner, :class_name => "User", :foreign_key => "owner_id" # helps retrieving the owner name
belongs_to :software, :class_name => "Parameter", :foreign_key => "software_id" # helps retrieving the software name
has_many :values
has_many :mappings_lists
### private functions definitions
private
### before filters
def set_code
self.code = name.gsub(/[^0-9A-Za-z]/, '_').upcase
end
end
.
# == Schema Information
#
# Table name: mappings_lists
#
# id :integer not null, primary key
# playground_id :integer
# code :string(255)
# name :string(255)
# description :text
# created_by :string(255)
# updated_by :string(255)
# owner_id :integer
# source_list_id :integer
# target_list_id :integer
# created_at :datetime
# updated_at :datetime
#
class MappingsList < ActiveRecord::Base
### scope
scope :pgnd, ->(my_pgnd) { where "playground_id=?", my_pgnd }
### before filter
before_create :set_code
### validation
validates :name, presence: true, uniqueness: true, length: { maximum: 100 }
validates :description, length: { maximum: 1000 }
validates :created_by , presence: true
validates :updated_by, presence: true
validates :playground_id, presence: true
belongs_to :playground
validates :playground, presence: true # validates that the playground exists
belongs_to :owner, :class_name => "User", :foreign_key => "owner_id" # helps retrieving the owner name
belongs_to :source_list, :class_name => "ValuesList", :foreign_key => "source_list_id" # helps retrieving the source list name
belongs_to :target_list, :class_name => "ValuesList", :foreign_key => "target_list_id" # helps retrieving the target list name
has_many :mappings
### private functions definitions
private
### before filters
def set_code
self.code = "#{source_list.code}_TO_#{target_list.code}"
end
end
routes contains this:
resources :values_lists do
resources :values
resources :mappings_lists
end
The controller is here:
class MappingsListsController < ApplicationController
# Check for active session
before_action :signed_in_user
# Retrieve current current mapping
before_action :set_mappings_list, only: [:show, :edit, :update, :destroy]
# Retrieve all lists of values
before_action :set_values_lists
# GET /mappings_list
# GET /mappings_list.json
def index
#mappings_list = MappingsList.pgnd(current_playground).order("name")
end
# GET /mappings_list/1
# GET /mappings_list/1.json
def show
### Retrieved by Callback function
end
# GET /mappings_list/new
def new
#mappings_list = MappingsList.new
end
# GET /mappings_list/1/edit
def edit
### Retrieved by Callback function
end
# POST /mappings_list
# POST /mappings_list.json
def create
#mappings_list = MappingsList.new(mappings_list_params)
#mappings_list.updated_by = current_user.login
#mappings_list.created_by = current_user.login
#mappings_list.playground_id = current_user.current_playground_id
#mappings_list.owner_id = current_user.id
respond_to do |format|
if #mappings_list.save
format.html { redirect_to #mappings_list, notice: 'List of mappings was successfully created.' }
format.json { render action: 'show', status: :created, location: #mappings_list }
else
format.html { render action: 'new' }
format.json { render json: #mappings_list.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /mappings_list/1
# PATCH/PUT /mappings_list/1.json
def update
### Retrieved by Callback function
#mappings_list.updated_by = current_user.login
respond_to do |format|
if #mappings_list.update(mappings_list_params)
format.html { redirect_to #mappings_list, notice: 'List of mappings was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #mappings_list.errors, status: :unprocessable_entity }
end
end
end
# DELETE /mappings_list/1
# DELETE /mappings_list/1.json
def destroy
### Retrieved by Callback function
#mappings_list.destroy
respond_to do |format|
format.html { redirect_to mappings_lists_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
# Check for active session
def signed_in_user
redirect_to signin_url, notice: "You must log in to access this page." unless signed_in?
end
def set_mappings_list
#mappings_list = MappingsList.pgnd(current_playground).find(params[:id])
end
# retrieve the list of values lists
def set_values_lists
#lists_of_values = ValuesList.all
end
# Never trust mappings from the scary internet, only allow the white list through.
def mappings_list_params
params.require(:mappings_list).permit(:name, :description, :source_list_id, :target_list_id)
end
end
Would the double reference to values_list turn my application into trouble ?
Thanks for your help,
Best regards,
Fred

In the class MappingsList < ActiveRecord::Base model
you have MappingList belonging to source_list.
which I believe is backwards:
class Order < ActiveRecord::Base
belongs_to :customer
end
#order = #customer.orders

OK, I got it !!!
In the index view, I cannot refer to one instance variable such as #mappings_list as I work on a list of instances.
The code works fine with :
<td><%= link_to mappings_list.source_list.name, mappings_list.source_list%>
Being a beginner at RoR, I hope my interpretation is right ...
Thanks for your help,
Best regards,
Fred

Related

Get Stripe charge information rails 5 create order

So I'm implementing Stripe and users are able to purchase successfully, however, I would like to get the charge information, last 4 card numbers, card type etc so I can generate receipts using https://github.com/excid3/receipts.
Here is what I have so far:
PaymentsController
class PaymentsController < ApplicationController
before_action :authenticate_user!
def create
token = params[:stripeToken]
#course = Course.find(params[:course_id])
#user = current_user
begin
charge = Stripe::Charge.create(
amount: (#course.price*100).to_i,
currency: "gbp",
source: token,
description: params[:stripeEmail],
receipt_email: params[:stripeEmail]
)
if charge.paid
Order.create(
course_id: #course.id,
user_id: #user.id,
Amount: #course.price
)
end
flash[:success] = "Your payment was processed successfully"
rescue Stripe::CardError => e
body = e.json_body
err = body[:error]
flash[:error] = "Unfortunately, there was an error processing your payment: #{err[:message]}"
end
redirect_to course_path(#course)
end
end
OrdersController
class OrdersController < ApplicationController
layout proc { user_signed_in? ? "dashboard" : "application" }
before_action :authenticate_user!
def index
#orders = Order.includes(:course).all
end
def show
#order = Order.find(params[:id])
respond_to do |format|
format.pdf {
send_data #order.receipt.render,
filename: "#{#order.created_at.strftime("%Y-%m-%d")}-aurameir-courses-receipt.pdf",
type: "application/pdf",
disposition: :inline
}
end
end
def create
end
def destroy
end
end
Order.rb
class Order < ApplicationRecord
belongs_to :course
belongs_to :user
validates :stripe_id, uniqueness: true
def receipt
Receipts::Receipt.new(
id: id,
subheading: "RECEIPT FOR CHARGE #%{id}",
product: "####",
company: {
name: "####",
address: "####",
email: "####",
logo: "####"
},
line_items: [
["Date", created_at.to_s],
["Account Billed", "#{user.full_name} (#{user.email})"],
["Product", "####"],
["Amount", "£#{amount / 100}.00"],
["Charged to", "#{card_type} (**** **** **** #{card_last4})"],
["Transaction ID", uuid]
],
font: {
normal: Rails.root.join('app/assets/fonts-converted/font-files/AvenirBook.ttf')
}
)
end
end
schema.rb
create_table "orders", force: :cascade do |t|
t.integer "user_id"
t.integer "course_id"
t.integer "stripe_id"
t.integer "amount"
t.string "card_last4"
t.string "card_type"
t.integer "card_exp_month"
t.integer "card_exp_year"
t.string "uuid"
t.index ["course_id"], name: "index_orders_on_course_id"
t.index ["user_id"], name: "index_orders_on_user_id"
end
How would I go about getting the charge information?
All the information you need is in the charge object you created.
if charge.paid
Order.create(
course_id: #course.id,
user_id: #user.id,
amount: #course.price,
card_last4: charge.source.last4,
card_type: charge.source.brand,
card_exp_month: charge.source.exp_month,
card_exp_year: charge.source.exp_year
)
end
See https://stripe.com/docs/api/ruby#charge_object for all the information available to you about a charge.
I guess you are not storing the Stripe Charge ID anywhere on successful payment.
My suggestion, store the charge ID in your Order record
if charge.paid
Order.create(
course_id: #course.id,
user_id: #user.id,
amount: #course.price,
stripe_charge_id: charge.id
)
end
In your receipt method you can fetch the stripe charge as follows:
def receipt
stripe_charge = Stripe::Charge.retrieve(stripe_charge_id)
...
end
The stripe_charge objects contains all the information and you can use whatever data you need.
Hope this helped.

rollback transaction error rails from google geocoding api

I am trying to create a review form in my rails app but when i click on the submit button, the form cannot be submitted.When i lookup the error in the terminal and i get this error. i searched the error but couldn't find any solution. did anyone had this issue before?:
Google API error: over query limit.
(0.1ms) rollback transaction
Update
I am not only getting the Google API error. sometimes i get this error and other time i only get rollback transaction only.
This is the Reviews Controller:
class ReviewsController < ApplicationController
# check if logged in
before_action :check_login, except: [:index, :show]
def index
# this is our list page for our reviews
#price = params[:price]
#cuisine = params[:cuisine]
#location = params[:location]
# start with all the reviews
#reviews = Review.all
# filtering by price
if #price.present?
#reviews = #reviews.where(price: #price)
end
# filter by cuisine
if #cuisine.present?
#reviews = #reviews.where(cuisine: #cuisine)
end
# search near the location
if #location.present?
#reviews = #reviews.near(#location)
end
end
def new
# the form for adding a new review
#review = Review.new
end
def create
# take info from the form and add it to the model
#review = Review.new(form_params)
# and then associate it with a user
#review.user = #current_user
# we want to check if the model can be saved
# if it is, we're go the home page again
# if it isn't, show the new form
if #review.save
flash[:succces] = "Your review was posted!"
redirect_to root_path
else
# show the view for new.html.erb
render "new"
end
end
def show
# individual review page
#review = Review.find(params[:id])
end
def destroy
# find the individual review
#review = Review.find(params[:id])
# destroy if they have access
if #review.user == #current_user
#review.destroy
end
# redirect to the home page
redirect_to root_path
end
def edit
# find the individual review (to edit)
#review = Review.find(params[:id])
if #review.user != #current_user
redirect_to root_path
elsif #review.created_at < 4.hours.ago
redirect_to review_path(#review)
end
end
def update
# find the individual review
#review = Review.find(params[:id])
if #review.user != #current_user
redirect_to root_path
else
# update with the new info from the form
if #review.update(form_params)
# redirect somewhere new
redirect_to review_path(#review)
else
render "edit"
end
end
end
def form_params
params.require(:review).permit(:title, :restaurant, :body, :score,
:ambiance, :cuisine, :price, :address)
end
end
This is the Review form page:
<%= simple_form_for #review do |f| %>
<%= f.input :title %>
<%= f.input :restaurant %>
<%= f.input :address %>
<%= f.input :body %>
<%= f.input :cuisine %>
<%= f.input :price %>
<%= f.input :score %>
<%= f.input :ambiance %>
<%= f.button :submit %>
<% end %>
The Review Model
class Review < ApplicationRecord
# add an association that has a 1-to-many relationship
has_many :comments
has_many :bookmarks
# add an association to the user
belongs_to :user
geocoded_by :address
after_validation :geocode
validates :title, presence: true
validates :body, length: { minimum: 10 }
validates :score, numericality: { only_integer: true, greater_than_or_equal_to: 0, less_than_or_equal_to: 10 }
validates :restaurant, presence: true
validates :address, presence: true
def to_param
id.to_s + "-" + title.parameterize
end
end
This is My Schema file
create_table "reviews", force: :cascade do |t|
t.string "title"
t.text "body"
t.integer "score"
t.string "restaurant"
t.integer "price"
t.string "cuisine"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "phone_number"
t.string "ambiance"
t.text "address"
t.float "latitude"
t.float "longitude"
t.integer "user_id"
end
You have two problems:
you are going over Google's Geocoding API quota as evidenced by the error message "over query limit"
Most likely because of that, your model cannot be saved is rolled back. The geocoding API call fails, it doesn't save.
I would check the Google API Console and see if you have actually hit their quota (possible if you're running multiple tests and you're on their free tier of service). If you have not hit the limit, file a support request with Google API.
You probably reached your Google API quota.
In model you have geocoded_by ... that is used by gem geocoder so have a look at that.
Google has per second limit as well as per day limit link

How can i return some message if before_validation method is false by Sinatra

I want to return some message that tell the user their password is not match with the confirm password field.
This is my code
post '/signup' do
user = User.new(params[:user])
if user.create_user
"#{user.list}"
else
"password not match"
end
end
require 'bcrypt'
class User < ActiveRecord::Base
include BCrypt
# This is Sinatra! Remember to create a migration!
attr_accessor :password
validates :username, presence: true, uniqueness: true
validates :email, :format => { :with => /\A[\w+\-.]+#[a-z\d\-]+(\.[a-z]+)*\.[a-z]+\z/i ,
:message => "Email wrong format" }
validates :email, uniqueness: true,presence: true
validates :encrypted_password, presence: true
validates :encrypted_password, length: { minimum: 6 }
before_validation :checking_confirm_password
def initialize(signup = {})
##signup = signup
super()
#username = signup["username"]
#email = signup["email"]
#password = signup["password"]
#confirm_password = signup["confirm_password"]
end
def create_user
p_p = Password.create(#password)
p_h ||= Password.new(p_p)
user_hash = {:username => #username,:email => #email, :encrypted_password => p_h}
return User.create(user_hash)
end
def list
User.all
end
private
def checking_confirm_password
if #password != #confirm_password
return false
end
end
end
So how can i specific the message that will send back to the user,
if their password is not match or the validation fail before create the dada?
Thanks!
The validations populate #user.errors with all validation errors with all validation errors by field, so you can easily return all validation errors at once like so:
post '/signup' do
user = User.new(params[:user])
if user.create_user
"#{user.list}"
else
user.errors.full_messages
end
end
Have a look at: http://edgeguides.rubyonrails.org/active_record_validations.html#working-with-validation-errors

Rails 4 ActiveModel::MissingAttributeError in OrdersController#create

I m trying to build a shopping cart for a website. I have the cart working so you can add to the cart. The problem I am having is when I try and checkout items in the cart and try to submit my order. I am getting the following error:
ActiveModel::MissingAttributeError in OrdersController#create
along with
can't write unknown attribute `order_id'
The problem is highlighting this piece of code in my orders_controller.rb file
respond_to do |format|
if #order.save
Cart.destroy(session[:cart_id]) session[:cart_id] = nil
I just cant seem to fix this error.
Below is the create method in my orders_controller.rb file
def create
#order = Order.new(order_params)
#order.add_line_items_from_cart(#cart)
respond_to do |format|
if #order.save
Cart.destroy(session[:cart_id])
session[:cart_id] = nil
format.html { redirect_to store_url,
notice: 'Thank you for your order.' }
format.json { render action: 'show', status: :created, location: #order }
else
format.html { render action: 'new' }
format.json { render json: #order.errors, status: :unprocessable_entity }
end
end
end
my migration file
class AddOrderToLineItem < ActiveRecord::Migration
def change
add_column :line_items, :order, :reference
end
end
my order.rb model
class Order < ActiveRecord::Base
has_many :line_items, dependent: :destroy
validates :name, :address, :email, presence: true
PAYMENT_TYPES = [ "Check", "Credit card", "Purchase order" ]
validates :pay_type, inclusion: PAYMENT_TYPES
def add_line_items_from_cart(cart)
cart.line_items.each do |item|
item.cart_id = nil
line_items << item
end
end
end
migration order table
class CreateOrders < ActiveRecord::Migration
def change
create_table :orders do |t|
t.string :name
t.text :address
t.string :email
t.string :pay_type
t.timestamps
end
end
end
Ok, I think I see the problem. Your migration for orders in line items probably didn't create the order_id column you expected, it probably created an orders column or something.
Verify that this is the case in the database. If so, undo your previous migration and try this instead:
class AddOrderToLineItem < ActiveRecord::Migration
def change
add_column :line_items, :order_id, :int
end
end
If not, please respond to this answer.

Nested Attributes for Mongoid has_one relationship

I am trying to create a contact which is a has_one relation to a client. I am doing this with nested attributes. I am properly building the contact within the "new" view/controller. When I go to save the contact it is telling me the contact must be present. So for some reason it is not creating the contact.
ERROR:
Validation failed - Contact can't be blank.
Params:
{
"utf8"=>"✓",
"authenticity_token"=>"ep6es356WY5dja7D7C5Kj8Qc0Yvuh3IN2Z1iGG08J7c=",
"client"=>{
"contact_attributes"=>{
"first_name"=>"Scott",
"last_name"=>"Baute"
},
"commit"=>"Create Client"
}
Models:
class Client
include Mongoid::Document
include Mongoid::Timestamps
attr_accessible :role, :contact_id, :contact_attributes
# Relationships
belongs_to :firm, validate: true
has_one :contact, validate: true, autosave: true
# Matters is custom relationship
has_many :client_roles
# Nested Attr
accepts_nested_attributes_for :contact
validates :contact_id, presence: true
# Fields
field :contact_id
field :test
end
class Contact
include Mongoid::Document
include Mongoid::Timestamps
include Mongoid::MultiParameterAttributes
#Relationships
belongs_to :client
field :first_name
field :last_name
end
Controller:
# GET /clients/new
# GET /clients/new.json
def new
#client = current_firm.clients.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: #client }
end
end
# POST /clients
# POST /clients.json
def create
#client = current_firm.clients.new(params[:client])
respond_to do |format|
if #client.save!
format.html { redirect_to client_path(#client.contact.id), notice: 'Client was successfully created.' }
format.json { render json: #client, status: :created, location: #client }
else
format.html { render action: "new" }
format.json { render json: #client.errors, status: :unprocessable_entity }
end
end
end
View:
- #client.build_contact unless #client_contact
= semantic_form_for #client, html: { class: "form-horizontal"} do |f|
.control-group
= render "contact_fields", f: builder
.form-actions
= f.submit class: "btn btn-primary"
I think you need delete your reference to contact_id in your Client model. The has_one association do the foreign_key in your contact. Not in your client. So when you create a contact from client, only a client_id is define in your contact no contact_id in your client.
So delete line :
validates :contact_id, presence: true
# Fields
field :contact_id
in your Client model.

Resources