rails 4 has_many association bug? - ruby-on-rails-4

i have an simple setup for a has_many association:
def Account
has_many :timers
end
def Timer
belongs_to :timer
end
if i try to build a timer i get this false behavior:
account.id => 1
account.timers.build => #<Timer id: nil, account_id: 0>
...of course it should be:
account.id => 1
account.timers.build => #<Timer id: nil, account_id: 1>
Because it's rails4 in tried the same with a fresh setup. and it works!
gems: pg, haml-rails, devise, simple_form, globalize3
Question: what could be the cause of breaking the Rails API ?

and the winner is:
gem 'globalize3', github: 'harlock/globalize3', branch: 'rails4-wip'

Related

RSpec Model Testing: Failures

I am teaching myself RSpec (v3.1.7). I have installed rspec with rails g rspec:install into an existing rails app - freshly created.
I created a model: rails g rspec:model zombie. Ran the migration and all went well.
In: app/models/zombie.rb:
class Zombie < ActiveRecord::Base
validates :name, presence: true
end
In: app/spec/models/zombie_spec.rb:
require 'rails_helper'
RSpec.describe Zombie, :type => :model do
it 'is invalid without a name' do
zombie = Zombie.new
zombie.should_not be_valid
end
end
In terminal when I ran (in the app dir): rspec spec/models I get:
F
Failures:
1) Zombie is invalid without a name
Failure/Error: zombie.should_not be_valid
NoMethodError:
undefined method `name' for #<Zombie id: nil, created_at: nil, updated_at: nil>
# ./spec/models/zombie_spec.rb:6:in `block (2 levels) in <top (required)>'
Im following a video tutorial and I followed the video (Testing with RSpec) down to the latter. I'm like losing weight on the 2nd chapter. Am I missing something? Is the video using an older version of rspec for their video tutorial?
In my migration file:
class CreateZombies < ActiveRecord::Migration
def change
create_table :zombies do |t|
t.timestamps
end
end
end
Your model don't know what name is since you did not define the attribute in your migration:
class CreateZombies < ActiveRecord::Migration
def change
create_table :zombies do |t|
t.string :name
t.timestamps
end
end
end
Then run:
rake db:migrate
Then this should work fine:
z = Zombie.new(name: 'foo')
z.name
=> 'foo'
I think you are missing the name attribute. The following migration file will add name attribute to zombie model:
class AddNameToZombies < ActiveRecord::Migration
def change
add_column :zombies, :name, :string
end
end
finally run the following commands:
rake db:migrate
rake db:test:prepare
and that's it

undefined method `attr_accessible' with Mongoid

I am using Rails 4.1.1, ruby 2.1, mongodb, mongoid as a wrapper, rails_admin for creating admin interfaces
I know that 'attr_accessible' no longer works for Rails4. So i have installed 'protected_attributes' gem. But still no success i am still getting warning in my console
[RailsAdmin] Could not load model Company, assuming model is non existing. (undefined method `attr_accessible' for Company:Class)
So, rails admin do not load the class Company because i have defined attr_accessible in the model. Here is my company model.
class Company
include Mongoid::Document
##employees_strength = {0 => '0-10', 1 => '11-50', 2 => '51-100', 3 => '101-500', 4 => '501-1000', 5 => '1000+', 6 => '5000+'}
field :name, type: String
field :website, type: String
field :domain_name, type: String
field :strength, type: Integer
has_many :employees
has_one :admin, :class_name => 'Employee', :dependent => :destroy, :inverse_of => :organization
#attr_accessible :name, :website, :domain_name, :strength#, :admin_attributes, :allow_destroy => true
attr_accessible :admin_attributes
accepts_nested_attributes_for :admin, :allow_destroy => true
end
Please any can body can help?
Thanks
Mongoid 4 (<= 4.0.2 at the time of writing) does not know about the ActiveModel::MassAssignmentSecurity module provided by protected_attributes gem.
As such you must include the behaviour in your models manually e.g.
class SomeDocument
include Mongoid::Document
include ActiveModel::MassAssignmentSecurity
field :some_field
attr_accessible :some_field
end
However, this gets tedious pretty quickly so a reasonable alternative is to include the module into the Mongoid::Document module before any of your models are defined.
module Mongoid
module Document
include ActiveModel::MassAssignmentSecurity
end
end

Rails Upgraded syntax error, unexpected '\n', expecting => (SyntaxError)

I have this query
has_many :unused_invitations, :class_name => 'Invitation', :foreign_key => 'inviter_id', :conditions => 'used = false'
I was using rails 3.2.17 and now I am upgrading to rails 4.0.4. I got this error
DEPRECATION WARNING: The following options in your User.has_many :unused_invitations declaration are deprecated: :conditions. Please use a scope block instead. For example, the following:
has_many :spam_comments, conditions: { spam: true }, class_name: 'Comment'
should be rewritten as the following:
has_many :spam_comments, -> { where spam: true }, class_name: 'Comment'
I solve it by modifying query
has_many :used_invitations, class_name: 'Invitation', foreign_key: 'inviter_id', -> { where used: false}
But Still I getting syntax error
syntax error, unexpected '\n', expecting => (SyntaxError)
What is wrong with query ? Will someone explain me about it. I have go this question but can't find the answer.
Solve this problem by updating the query
has_many :used_invitations, -> { where used: false}, class_name: 'Invitation', foreign_key: 'inviter_id'

Rails 4 routes with single table inheritance and self references

I've been jumping between design patterns, firstly trying polymorphic, now landing on STI. The main goal is to implement a Server > Host > Guest model where a Server has Hosts, Hosts have Guests and each able to have Posts. Although not the main purpose of the question any ideas in the design matter would be helpful as this is my first rails or ruby project.
What I have now is:
class Device
has_may :children, :class_name => "Device", :foreign_key => "parent_id"
belongs_to :parent, :class_name => "Device"
has_many :posts
end
class Server,Host,Guest < Device
end
STI is used because Server,Host,Guest basically have the same attributes.
I'm having trouble setting up the routes and controllers so I could view a Server's children which would be of type Host or to create a new Server's Host.
First, a good thing would be to add the following things, making everything easier to use for you :
class Device < ActiveRecord::Base
has_many :children, :class_name => "Device", :foreign_key => "parent_id"
has_many :servers, -> { where type: "Server" }, :class_name => "Device", :foreign_key => "parent_id"
has_many :hosts, -> { where type: "Host" }, :class_name => "Device", :foreign_key => "parent_id"
has_many :guests, -> { where type: "Guest" }, :class_name => "Device", :foreign_key => "parent_id"
belongs_to :parent, :class_name => "Device"
has_many :posts
end
With that, you will be able to do server.hosts, etc, which is quite convenient.
Then, you should move each subclass (Server, Host, Guest) to its own file due to Rails loading system. You can try to access the model Server in the console, you will get an undefined error. To fix it, you need to load the model Device, or simply move each subclass in a different file.
Finally, for the routing/controller part, I will advise you to read this post I wrote about common controller for STI resources : http://samurails.com/tutorial/single-table-inheritance-with-rails-4-part-2/.
Note that this is the second part, for more details check out the other articles.

Deprecated warning with has_many_through statement in Rails 4

I have the following relationship
class User
has_many :relationships
has_many :friends, :through => :relationships, -> select: 'friends.*, relationships.weight', order: 'weight DESC'
When I upgrade to Rails 4, I got the following warning:
DEPRECATION WARNING: The following options in your Service.has_many :friends declaration are deprecated: :order,:select.
How should I fix this? In general, is there a working in progress reference for Rails 4?
In Rails 4, any of the options that you would see in a normal User.where(...) style query would now go in a lambda. This includes :order and :select:
has_many :friends, -> { select("friends.*, relationships.weight").order("weight desc") }, :through => :relationships
Note that the Proc does need to be the second parameter to has_many, so the :through => part needs to stay at the end.