Rails testing has_many association failure - ruby-on-rails

I have 2 models. A User and a Task. Here's the code for them both:
class User < ActiveRecord::Base
has_many :tasks
has_many :assigned_tasks, :class_name => 'Task', :foreign_key => 'assigned_user_id'
end
class Task < ActiveRecord::Base
belongs_to :user
belongs_to :assigned_user, :class_name => 'User', :foreign_key => 'assigned_user_id'
end
The schema is quite obvious, but for consistency, this is how it looks:
ActiveRecord::Schema.define(:version => 20110925050945) do
create_table "tasks", :force => true do |t|
t.string "name"
t.integer "user_id"
t.integer "assigned_user_id"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "users", :force => true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
end
end
I've added a test case for the assigned_tasks relationship. It looks like this:
class UserTest < ActiveSupport::TestCase
test "assigned tasks" do
u1 = User.create(:name => 'john')
u2 = User.create(:name => 'dave')
assert_empty u2.assigned_tasks # LOOK AT ME
task = u1.tasks.create(:name => 'some task', :assigned_user_id => u2.id)
assert_equal 1, u2.assigned_tasks.size
end
end
Now, this test case fails. It fails on the last assertion. If I remove the previous assertion (marked 'LOOK AT ME'), this test passes fine. It also passes fine if I change this line to assert u2.assigned_tasks. Meaning it appears to break when, and only when, empty? is called against u2.assigned_tasks. Where that assertion passes, the following one fails. Here's the failure:
UserTest:
FAIL assigned tasks (0.12s)
<1> expected but was
<0>.
test/unit/user_test.rb:12:in `block in <class:UserTest>'
So, it appears once empty? is called on the original u2.assigned_tasks Array, the task is not actually added/associated with it's assigned user. This however appears to work fine in console.
Apologies if I'm completely overlooking something simple here, but I really can't make any sense of this. Any points in the right direction would be extremely helpful. Thanks
PS: Rails 3.1 with a vanilla application

You need to reload the assigned_tasks, or u2.
# This line causes assigned_tasks to be loaded and cached on u2. Not the calling
# of empty?, but rather the loading of the association.
assert_empty u2.assigned_tasks
# but then you actually make the task here
task = u1.tasks.create(:name => 'some task', :assigned_user_id => u2.id)
# so when this assertion happens, u2 already has an empty set of tasks cached,
# and fails
assert_equal 1, u2.assigned_tasks.size
# however either of these should pass
assert_equal 1, u2.assigned_tasks(true).size
assert_equal 1, u2.reload.assigned_tasks.size
The inverse_of option serves to improve in-memory association behavior, and might also solve your problem (without reloading). Read about that here. It would look something like this (but again I'm not positive it will work in this case):
# on User
has_many :assigned_tasks, ..., :inverse_of => :assigned_user
# on Task
belongs_to :assigned_user, ..., :inverse_of => :assigned_tasks
# in your test you might have to change the task creation to:
u1.tasks.create(:name => 'some task', :assigned_user => u2)

Related

In a Rails Model, will belongs_to cause a rollback if not defined?

Model:
class UserPosition < ApplicationRecord
belongs_to :user
belongs_to :job_title
end
UserPosition's schema:
t.integer :user_id
t.integer :company_id
t.integer :industry_id
t.integer :department_id
t.integer :job_title_id
t.string :job_title_custom
user_positions_controller.rb
def create
#user_position = UserPosition.find_or_create_by(user_id: current_user.id)
#user_position.update_attributes({
:industry_id => params[:industry_id],
:department_id => params[:department_id],
:job_title_id => params[:job_title_id],
:job_title_custom => params[:job_title_custom]
})
I need UserPosition to either create a record with:
user_id
job_title_custom
OR
t.integer :user_id
t.integer :company_id
t.integer :industry_id
t.integer :department_id
t.integer :job_title_id
Currently, if I try to create a UserPosition with just user_id & job_title_custom
It doesn't work, the logs show ROLLBACK the error message is:
#messages={:job_title=>["must exist"]}
What am I doing wrong here? I think it could be because job_title has a relationship defined in the model but the Rails Guide says that they are optional, so I'm not sure.
Turns out this is a new Rails 5 behavior.
"In Rails 5, whenever we define a belongs_to association, it is required to have the associated record present by default after this change.
It triggers validation error if associated record is not present."
"In Rails 4.x world To add validation on belongs_to association, we need to add option required: true ."
"Opting out of this default behavior in Rails 5. We can pass optional: true to the belongs_to association which would remove this validation check."
Full Answer: http://blog.bigbinary.com/2016/02/15/rails-5-makes-belong-to-association-required-by-default.html

Trouble with self referential model in Rails

I have a model named User and I want to be able to self reference other users as a Contact. In more detail, I want a uni-directional relationship from users to other users, and I want to be able to reference an owned user of one user as a 'contact'. ALSO, i want to have information associated with the relationship, so I will be adding fields to the usercontact relation (I just edited this sentence in).
I attempted to do this while using the answer to this question as a guide.
Here is the User model:
user.rb
class User < ActiveRecord::Base
attr_accessible(:company, :email, :first_name, :last_name,
:phone_number, :position)
has_many(:user_contacts, :foreign_key => :user_id,
:dependent => :destroy)
has_many(:reverse_user_contacts, :class_name => :UserContact,
:foreign_key => :contact_id, :dependent => :destroy)
has_many :contacts, :through => :user_contacts, :source => :contact
end
I also created the model UserContact as a part of connecting contacts to users:
usercontact.rb
class UserContact < ActiveRecord::Base
belongs_to :user, :class_name => :User
belongs_to :contact, :class_name => :User
end
Here is the create_users.rb migration file i used:
create_users.rb
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :first_name
t.string :last_name
t.string :phone_number
t.string :email
t.string :company
t.string :position
t.timestamps
end
end
end
And here is the create_users_contacts.rb migration:
create_users_contacts.rb
class CreateUsersContacts < ActiveRecord::Migration
def up
create_table :users_contacts, :force => true do |t|
t.integer :user_id, :null => false
t.integer :contact_id, :null => false
t.boolean :update, :null => false, :default => false
end
# Ensure that each user can only have a unique contact once
add_index :users_contacts, [:user_id, :contact_id], :unique => true
end
def down
remove_index :users_contacts, :column => [:user_id, :contact_id]
drop_table :users_contacts
end
end
However, for reasons unknown to me, I believe something has gone awry in the linking since on my users index page, I have a column using <td><%= user.contacts.count %></td>, but I get this error from the line when I attempt to load the page:
uninitialized constant User::UserContact
I think the issue may be something to do with the fact that I want to name users associated with another user as contacts, because I cannot find other examples where that is done, and as far as I can tell I am doing everything properly otherwise (similarly to other examples).
The closest similar problem that I found was outlined and solved in this question. The issue was incorrect naming of his connecting model, however I double checked my naming and it does not have that asker's problem.
Any help is appreciated, let me know if any other files or information is necessary to diagnose why this is occurring.
EDIT
After changing usercontact.rb to user_contact.rb, I am now getting this error:
PG::Error: ERROR: relation "user_contacts" does not exist
LINE 1: SELECT COUNT(*) FROM "users" INNER JOIN "user_contacts" ON "...
^
: SELECT COUNT(*) FROM "users" INNER JOIN "user_contacts" ON "users"."id" = "user_contacts"."contact_id" WHERE "user_contacts"."user_id" = 1
EDIT TWO
The issue was that my linking table, users_contacts, was misnamed, and should have been user_contacts! so I fixed it, and now it appears to work!!
You need to rename your usercontact.rb to user_contact.rb
This is naming convention rails autoload works with.

Rails + model.rb issues -- rails console error message -- "Object doesn't support #inspect"

EDIT3:
To wit, DO NOT put migration code in your model.rb files!!!
EDIT2: THE QUESTION (?) :
does ANY migration code belong in a model.rb file?
EDIT: Just mentioning extra (system/config/etc) information that I need to share in order to get a good answer to this question from someone (even if it's not you) would be greatly appreciated. (1-ups for good tips on stack overflow optimization strategies)
First of all, here is the command prompt activity:
C:\Users\davo\Desktop\RailsProjects\simple_cms>rails c
Loading development environment (Rails 3.2.3)
irb(main):001:0> subject = Subject.find(1)
←[1m←[36mSubject Load (1.0ms)←[0m ←[1mSELECT `subjects`.* FROM `subjects` WHERE `subjects`.`id` = 1
LIMIT 1←[0m
=> #<Subject id: 1, name: "Initial Subject", position: 1, visible: true, created_at:"2012-05-18 01:00:26", updated_at: "2012-05-18 01:11:21">
irb(main):002:0> subject.pages
(Object doesn't support #inspect)
The basic schema is that we have two models here, page.rb and subject.rb. Subject is the parent of Page, as you will see. Here are the two models.
Guide to viewing this code: I think all that is relevant to this problem in these two models are the has_many and belongs_to tags. And I admit, I feel like there should be some foreign keys here. Should there be foreign keys here? Or is that wrong too?
subject.rb
class Subject < ActiveRecord::Base
# attr_accessible :title, :body
has_many :pages
scope :visible, where(:visible => true)
scope :invisible, where(:visible => false)
scope :search, lambda {|query| where(["name LIKE ?", "%#{query}%"])}
end
page.rb
class Page < ActiveRecord::Base
has_many :sections
belongs_to :subject
# attr_accessible :title, :body
create_table "Pages" do |t|
t.string "name"
t.string "permalink"
t.integer "position"
t.boolean "visible?"
end
end
I'm really new at this, so please forgive me if I didn't give you some piece of information that you need. Please let let know what extra information you need, I'm not sure where the error is coming from but I know this is a model (M....VC) issue. 95% on that one.
You have a migration in your model.
create_table "Pages" do |t|
t.string "name"
t.string "permalink"
t.integer "position"
t.boolean "visible?"
end
Should be in ./db/migrate/{timestamp}_create_pages.rb. This file was generated for you if you did rails g model page
You also need a subject_id column to store the relation to subject
class CreatePages < ActiveRecord::Migration
def change
create_table :pages do |t|
t.integer :subject_id
t.string :name
t.string :permalink
t.integer :position
t.boolean :visible?
t.timestamps
end
end
end

Ruby on Rails allow mass assignment for an RSpec test

I'm testing my Rails application with RSpec, but then I ran into a problem. I want a consistent database, therefore I imposed a constraint that some columns can not be null.
I have a Comment model and a comment may be an answer to another comment. Morevoer a comment has an IP address which should not be null. This is the migration:
create_table :comments do |t|
t.string :name, :limit => 20, :null => false
t.string :comment, :limit => 8192, :null => false
t.string :ip, :null => false
t.integer :answer_to_comment_id
end
Then I created a Comment model with only name and comment accessible
class Comment < ActiveRecord::Base
attr_accessible :name, :comment
belongs_to :answer_to, :class_name => "Comment",
:foreign_key => "answer_to_comment_id"
has_many :answers, :class_name => "Comment",
:foreign_key => "answer_to_comment_id",
:dependent => :destroy
end
My factories.rb looks like this:
Factory.define :comment do |comment|
comment.name "test"
comment.comment "test"
comment.ip "0.0.0.0"
end
Now I have the following problem in the RSpec test comment_spec.rb
describe "some test" do
before(:each) do
#answer = #comment.answers.create(Factory.attributes_for(:comment))
end
end
This will fail, because :ip is not in the attr_accessible list and therefore ActiveRecord can't create the record in the database. I could add :ip to the list, but this may cause some security issues because of mass assignment. Or I could add the :ip manually, but this could become a lot of work if there were more attributes like ip
So I look for a possibility to bypass the attr_accessible list. Or if you have a better design pattern, please let me know
Thank you
I ran across this question while searching for a solution to the same problem. I know it's very old, but for what it's worth I dug through the code and decided to solve the problem this way:
before :each do
ActiveModel::MassAssignmentSecurity::WhiteList.any_instance.stub(:deny?).and_return(false)
end
Perhaps this will come in handy to someone else who winds up here.
Just use:
describe "some test" do
before(:each) do
#answer = #comment.answers << Factory(:comment)
end
end
or if you need more than one comment, say n
describe "some test" do
before(:each) do
#answer = #comment.answers = FactoryGirl.create_list(:comment, n)
end
end
I basically use a variation of this (along with a few other tweak) during testing.
(But Fabio's answer is cleaner--that's one of the things factories are for, making things--not just a holder for attributes :)

How do you seed models with HABTM relationships to other seeded models

I'm working on my first Rails(3) App, and looking to seed a bunch of data.
The issue I'm having is that I want to seed some models that have a has_and_belongs_to_many relationship with other models I've just seeded. I'm doing what seems right, but I'm not getting the results I'm expecting.
I have an Asana model (simplified):
class Asana < ActiveRecord::Base
has_and_belongs_to_many :therapeutic_foci
end
and the TherapeuticFocus model:
class TherapeuticFocus < ActiveRecord::Base
has_and_belongs_to_many :asanas
end
In my db/seeds.rb, I create some TherapeuticFoci:
tf = TherapeuticFocus.create([
{:name => 'Anxiety'},
{:name => 'Asthma'},
{:name => 'Fatigue'},
{:name => 'Flat Feet'},
{:name => 'Headache'},
{:name => 'High Blood Pressure'},
{:name => 'Stress'} ])
Then create an Asana:
asanaCreate = Asana.create!([
{ :english_name => 'Mountain Pose',
:traditional_name => 'Tadasana',
:pronunciation => 'TadaSANA',
:deck_set => 'Basic',
:type => 'Standing',
:therapeutic_foci => TherapeuticFocus.where("name in ('Stress','Flat Feet')")}
])
The result is that the TherapeuticFocus models are created, the Asana is created, but it doesn't create the relationships to the TherapeuticFocus models. The resulting array is empty.
If I run
TherapeuticFocus.where("name in ('Stress','Flat Feet')")
in the rails console, I get the expected two records:
irb(main):010:0> TherapeuticFocus.where("name in ('Stress','Flat Feet')")
=> [#<TherapeuticFocus id: 6, name: "Flat Feet", description: nil, created_at: "2010-10-11 01:48:02", updated_at: "2010-10-11 01:48:02">,
#<TherapeuticFocus id: 19, name: "Stress", description: nil, created_at: "2010-10-11 01:48:02", updated_at: "2010-10-11 01:48:02">]
So, how does one do this?
Or, is there a better way to do this?
Thanks!
POST ANSWER:
I had already added the inflection:
ActiveSupport::Inflector.inflections do |inflect|
inflect.irregular 'focus', 'foci'
end
My migration for the join tables looks like:
create_table :asanas_therapeutic_foci, :id => false do |t|
t.references :asana, :therapeutic_focus
end
I'll try changing this to t.belongs_to instead of t.references and see if that works.
Did you register the pluralization for “focus”? It is not defined by default, so you will need to define it (usually in config/initializers/inflections.rb):
ActiveSupport::Inflector.inflections do |inflect|
inflect.irregular 'focus', 'foci'
end
You also need to make sure your migration has defined the correct join table for the HABTM association. Here is the pertinent part of the “up” migration that I used:
create_table :asanas do |t|
t.string :english_name
t.string :traditional_name
t.string :pronunciation
t.string :deck_set
t.string :type
end
create_table :therapeutic_foci do |t|
t.string :name
end
create_table :asanas_therapeutic_foci, :id => false do |t|
t.belongs_to :asana
t.belongs_to :therapeutic_focus
end
I used the models as you quoted.
With those bits in place, I was able to load your seed definitions.

Resources