Rails - Nested association - scope order - with I18n translation - ruby-on-rails-4

I am using globalize and i18n.
I have the following models:
class Result < ActiveRecord::Base
belongs_to :test
...
scope :sort_with_teaser, lambda { |direction|
joins(test: :test_translations).
where(test_translations: {locale: 'fr'}).
order("test_translations.teaser #{ direction }")
}
end
class Test < ActiveRecord::Base
translates :teaser
...
end
So with Globalize and I18n, I have also a test_translations table.
I want to implement a scope sort_with_teaser inside my Result model to sort result with their result.test.teaser. But I this code doesn't work.
Any help welcome.

Ok I just fixed it :
joins(test: :translations).where(test_translations: {locale: 'fr'}).order("test_translations.teaser #{ direction }")

Related

Strong parameter does not work when i new model and controller in spree '3-0-stable' and rails 4.2

How to add new fields for spree::pre_order in Spree-3.0 + Rails4 ?
Like my old customization: ==========================
app/Controllers/spree/api/pre_order_controller
class PreOrderController < Spree::Api::BaseController
def create
#pre_order = PreOrder.new(pre_order_params)
if #pre_order.save
end
respond_with(#pre_order)
end
private
def pre_order_params
params.require(:pre_order).permit(:user_id,:is_order_created)
end
end
I Got following Error
{
"exception": "param is missing or the value is empty: pre_order"
}
Please let me know your comments.

Deeply associated column_names in counter_culture sum

When defining column_names in a counter_culture method, is it possible to deeply associate? In the docs and examples its always an attribute belonging to the model that is used to determine column_names. But, what if the attribute belongs to an associated model?
example, this works
# Account model
counter_culture :user,
column_name: Proc.new { |account| account.has_billable_hours? ? 'billed_hours_sum' : nil },
delta_column: 'billed_hours',
column_names: { ["account.billed_hours > ?", 0] => "billed_hours_sum" }
associated example
# Account model
counter_culture :user,
column_name: Proc.new { |account| account.clients.has_billable_hours? ? 'billed_hours_sum' : nil },
delta_column: 'billed_hours',
column_names: { ["accounts.clients.billed_hours > ?", 0] => "billed_hours_sum" }
If, for the above, you could (you can't) use joins in the column_names method it would look like this
joins(:account=>:client).where("accounts.clients.billed_hours > ?", 0)
The second example illustrates my question. How do you define the column_names when the attribute you need to evaluate does not belong to the parent model, but an associated model?
column_names is only need when calling counter_culture_fix_counts. So, I just 86'ed the column_names option from the the method call and created a rake task to update that counter/column manually.

Rails 4 - Circular dependency detected while autoloading constant Users::TodosController

besides using devise for authentification w standard Devise routes.. , I added the follwoing
namespace :users do
resources :todos
end
to magane todos for the current user.
the generated routes are fine ...
users_todos GET /users/todos(.:format) users/todos#index
POST /users/todos(.:format) users/todos#create
new_users_todo GET /users/todos/new(.:format) users/todos#new
edit_users_todo GET /users/todos/:id/edit(.:format) users/todos#edit
users_todo GET /users/todos/:id(.:format) users/todos#show
PATCH /users/todos/:id(.:format) users/todos#update
PUT /users/todos/:id(.:format) users/todos#update
DELETE /users/todos/:id(.:format) users/todos#destroy
once the user is logged in , he is redirected to his todos#index list .. (users_todos_url)
def after_sign_in_path_for(resource)
....
elsif resource.is_a?(User) && Settings.permit_user_login
stored_location_for(resource) || users_todos_url
....
but then , the error is raised .... and it's stated in the log :
Started GET "/users/todos" for 127.0.0.1 at 2014-07-22 14:29:15 +0200
LoadError - Unable to autoload constant Users::TodosController, expected /Users/yves/bitbucket/railsTests/todoapp/app/controllers/users/todos_controller.rb to define it:
I have a users/todo_controller.rb
class User::TodosController < ApplicationController
..
def index
sort_order = "updated_at DESC , title"
todos = Todo.where(:user_id => current_user[:id]).order(sort_order).page params[:page]
end
Log says todo s _controller.rb.
Controller names are plural form.

Rails Fabricator gem with mongoid has_many_and_belongs_to relationship

I'm trying to fabricate a class that has nested elements and has HMABT relationship with another class. Here are my classes
class Quote
has_and_belongs_to_many :providers
belongs_to :user
class Provider
has_and_belongs_to_many :quotes
belongs_to :user
embeds_many :workdones
class Workdone
embedded_in :provider
embeds_many :prices
class Price
embedded_in :workdone
These are my fabricators
Fabricator(:price) do
workdone
variation_id {Fabricate(:variation)._id}
price {12}
discount {5}
end
usern = Faker::Name.last_name
uidn = Faker::Number.number(10)
Fabricator(:user) do
uid 123456
username {usern }
email {Faker::Internet.email}
username_or_uid { [ usern , uidn] }
provider {'facebook'}
name {Faker::Name.name }
gender {'male'}
birthday { Time.at(rand * (18.years.ago.to_f - 50.years.ago.to_f) + 50.years.ago.to_f).to_date }
end
Fabricator(:workdone) do
provider
workdonecount {1}
quotegivencount {1}
rating {5}
active {true}
give_price {true}
end
Fabricator(:provider) do
user
business_address {"Bostanlı Mh., 6352. Sokak No:15, 35480 İzmir, Türkiye"}
business_description {"Biz de sadece epilasyon işleriyle uğraşıyoruz ve bu işlerin
quote(count: 1)
Fabricator(:quote) do
user
providers(count: 3) { Fabricate(:price).workdone.provider }
share_on_facebook_timeline {false}
customer_address {"Bostanlı Mh., 6352. Sokak No:15, 35480 İzmir, Türkiye"}
description {"dasdsadasdsad sadadsssssdsadsasdas"}
location {[27.094637499999976,38.4621336 ] }
variation_id { Fabricate(:variation)._id}
end
When I fabricate quote with Fabricate(:quote)
It gives out this error message
Quote#give_quote works
Failure/Error: Unable to find matching line from backtrace
SystemStackError:
stack level too deep
When I remove quote(count: 1) from provider fabricator it gives out this error.
This works on rails console by the way - providers are created.
Failure/Error: quote = Fabricate(:quote)
TypeError:
no implicit conversion of nil into String
# ./spec/fabricators/quote_fabricator.rb:4:in `block (2 levels) in <top (required)>'
# ./spec/models/quote_spec.rb:51:in `block (3 levels) in <top (required)>'
When I completely remove the providers(count: 3) { Fabricate(:price).workdone.provider }
association from quote fabricator tests pass but of course providers are not created
Does anyone have idea how I can create providers?
Would it be possible to pull three providers from the generated user for this quote? You could do so with the below Fabricator definition.
Fabricator(:quote) do
user
providers { |attrs| attrs[:user].providers.sample(3) }
share_on_facebook_timeline false
customer_address "Bostanlı Mh., 6352. Sokak No:15, 35480 İzmir, Türkiye"
description "dasdsadasdsad sadadsssssdsadsasdas"
location [27.094637499999976,38.4621336]
variation_id { Fabricate(:variation)._id }
end
Also, you only need to use the block syntax if you are dynamically generating values. In the case of all your static values, just passing them in directly (as in the above) will give you a little less overhead.

Scope not working in ActiveAdmin ROR

To allow me to quickly filter records in ActiveAdmin i've defined scopes on my model. I have "shipped" and "unshipped" scopes as below. For some reason the "Shipped" scope is working as expected and shows the number of shipped items but the "Unshipped" scope doesn't do anything, it doesn't seem to know what is unshipped. It seems that i have to check and then uncheck "Shipped" checkbox in order for it to know that it's unshipped??
ORDER MODEL
class Order < ActiveRecord::Base
scope :shipped, where(:shipped => true)
scope :unshipped, where(:shipped => false)
end
ADMIN ORDER MODEL
ActiveAdmin.register Order do
scope :all, :default => true
scope :shipped
scope :unshipped
index do
selectable_column
column "Status", :sortable => :shipped do |s|
status_tag((s.shipped? ? "Shipped" : "Unshipped"), (s.shipped? ? :ok : :warning))
end
end
end
Can anyone see what the problem is?
Many Thanks
Is that the actual code from your model?
It should be:
scope :shipped, -> { where(shipped: true) }
scope :unshipped, -> { where(shipped: false) }
Realised that shipped was not by default set to false so fixed the issue by doing so in the Orders table.