FactoryGirl 3.x has_many through association - ruby-on-rails

I've seen a couple examples of the older syntax, but I can't find an example using the new 3.x syntax (one such older example: factory girl multiple has_many through's).
Models
class RawPosition < ActiveRecord::Base
has_many :position_translations
has_many :specific_positions, through: :position_translations
end
class SpecificPosition < ActiveRecord::Base
has_many :position_translations
has_many :raw_positions, through: :position_translations
end
class PositionTranslation < ActiveRecord::Base
belongs_to :raw_position
belongs_to :specific_position
end
Factories
factory :raw_poisition_multiple, class: RawPosition do
raw_input "WR/QB"
sport_type_id 5
after_create do |a|
#a.specific_positions.create({specific_position: "WR"})
#a.specific_positions.create({specific_position: "QB"})
FactoryGirl.create(:specific_position, raw_position: a)
FactoryGirl.create(:qb_specific_position, raw_position: a)
end
end
factory :specific_position do
specific_position "WR"
end
factory :qb_specific_position do
specific_position "QB"
end
Spec
describe "WR/QB" do
before do
#player.player_dict['POS'] = "WR/QB"
FactoryGirl.create(:raw_poisition_multiple)
#player.clean_position(#player_to_team_history)
end
....
end
If I uncomment the lines from the raw_position_multiple factory and comment out the FactoryGirl lines in the after_create block, things work fine. I'd just like the ability to use a factory to create the associations.

I was able to get past a similar issue by reloading the root model after creating the associations. In your example, that would mean adding:
a.reload
to the end of your after_create block.

Related

How to detect changes in has_many through association?

I have the following models.
class Company < ApplicationRecord
has_many :company_users
has_many :users, :through => :company_users
after_update :do_something
private
def do_something
# check if users of the company have been updated here
end
end
class User < ApplicationRecord
has_many :company_users
has_many :companies, :through => :company_users
end
class CompanyUser < ApplicationRecord
belongs_to :company
belongs_to :user
end
Then I have these for the seeds:
Company.create :name => 'Company 1'
User.create [{:name => 'User1'}, {:name => 'User2'}, {:name => 'User3'}, {:name => 'User4'}]
Let's say I want to update Company 1 users, I will do the following:
Company.first.update :users => [User.first, User.second]
This will run as expected and will create 2 new records on CompanyUser model.
But what if I want to update again? Like running the following:
Company.first.update :users => [User.third, User.fourth]
This will destroy the first 2 records and will create another 2 records on CompanyUser model.
The thing is I have technically "updated" the Company model so how can I detect these changes using after_update method on Company model?
However, updating an attribute works just fine:
Company.first.update :name => 'New Company Name'
How can I make it work on associations too?
So far I have tried the following but no avail:
https://coderwall.com/p/xvpafa/rails-check-if-has_many-changed
Rails: if has_many relationship changed
Detecting changes in a rails has_many :through relationship
How to determine if association changed in ActiveRecord?
Rails 3 has_many changed?
There is a collection callbacks before_add, after_add on has_many relation.
class Project
has_many :developers, after_add: :evaluate_velocity
def evaluate_velocity(developer)
#non persisted developer
...
end
end
For more details: https://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#label-Association+callbacks
You can use attr_accessor for this and check if it changed.
class Company < ApplicationRecord
attr_accessor :user_ids_attribute
has_many :company_users
has_many :users, through: :company_users
after_initialize :assign_attribute
after_update :check_users
private
def assign_attribute
self.user_ids_attribute = user_ids
end
def check_users
old_value = user_ids_attribute
assign_attribute
puts 'Association was changed' unless old_value == user_ids_attribute
end
end
Now after association changed you will see message in console.
You can change puts to any other method.
I have the feelings you are asking the wrong question, because you can't update your association without destroy current associations. As you said:
This will destroy the first 2 records and will create another 2 records on CompanyUser model.
Knowing that I will advice you to try the following code:
Company.first.users << User.third
In this way you will not override current associations.
If you want to add multiple records once try wrap them by [ ] Or ( ) not really sure which one to use.
You could find documentation here : https://guides.rubyonrails.org/association_basics.html#has-many-association-reference
Hope it will be helpful.
Edit:
Ok I thought it wasn't your real issue.
Maybe 2 solutions:
#1 Observer:
what I do it's an observer on your join table that have the responsability to "ping" your Company model each time a CompanyUser is changed.
gem rails-observers
Inside this observer call a service or whatever you like that will do what you want to do with the values
class CompanyUserObserver < ActiveRecord::Observer
def after_save(company_user)
user = company_user.user
company = company_user.company
...do what you want
end
def before_destroy(company_user)
...do what you want
end
end
You can user multiple callback in according your needs.
#2 Keep records:
It turn out what you need it keep records. Maybe you should considerate use a gem like PaperTrail or Audited to keep track of your changes.
Sorry for the confusion.

Rails FactoryGirl for model that belongs_to 2 other models

I have 3 following models like this:
# model/timeline.rb
class Timeline
belongs_to :series
belongs_to :creator
end
def series_belongs_to_creator
if creator_id
creator = Creator.find_by id: creator_id
related_series = creator.series.find_by id: series_id
errors.add(:series_id, :not_found_series) unless related_series
end
end
# model/creator.rb
class Creator
has_many :timelines
has_many :series, through: :contents
end
# model/series.rb
class Series
has_many :timelines
has_many :creators, through: :contents
end
This is not many to many relation, timelines table has two fields creator_id and series_id beside another fields. creator_id and series_id must be entered when create Timeline and i have a method series_belongs_to_creator to validates series_id must belong to creator_id to create successful.
So how should I write factory for timeline model if using FactoryGirl. Im so confused about Unit test in Rails.
If you're using Rails 5, you have to keep in mind that belongs_to is no longer optional by default: https://blog.bigbinary.com/2016/02/15/rails-5-makes-belong-to-association-required-by-default.html
So creator_id will always need to be present unless you specify the relation is optional.
For the factories, you're going to end up with something like this (FactoryGirl was recently renamed to FactoryBot):
http://www.rubydoc.info/gems/factory_bot/file/GETTING_STARTED.md#Associations
FactoryBot.define do
factory :timeline do
creator
series
end
end
FactoryBot.define do
factory :creator do
...
end
end
FactoryBot.define do
factory :series do
...
end
end

Creating objects with associations in Rails

In my Rails app I have Clients and Users. And Users can have many Clients.
The models are setup as so:
class Client < ApplicationRecord
has_many :client_users, dependent: :destroy
has_many :users, through: :client_users
end
class User < ApplicationRecord
has_many :client_users, dependent: :destroy
has_many :clients, through: :client_users
end
class ClientUser < ApplicationRecord
belongs_to :user
belongs_to :client
end
So if I wanted to create a new client that had the first two users associated with it how would I do it?
e.g.
Client.create!(name: 'Client1', client_users: [User.first, User.second])
Trying that gives me the error:
ActiveRecord::AssociationTypeMismatch: ClientUser(#70142396623360) expected, got #<User id: 1,...
I also want to do this for an RSpec test. e.g.
user1 = create(:user)
user2 = create(:user)
client1 = create(:client, client_users: [user1, user2])
How do I create a client with associated users for in both the Rails console and in an RSpec test?
If you do not want to accept_nested_attributes for anything, as documented here you can also pass block to create.
Client.create!(name: 'Client1') do |client1|
client1.users << [User.find(1), User.find(2), User.find(3)]
end
Try this. It should work
Client.create!(name: 'Client1').client_users.new([{user_id:
User.first},{user_id: User.second}])
You can do this with the following code:
user1 = create(:user)
user2 = create(:user)
client1 = create(:client, users: [user1, user2])
See ClassMethods/has_many for the documentation
collection=objects
Replaces the collections content by deleting and adding objects as
appropriate. If the :through option is true callbacks in the join
models are triggered except destroy callbacks, since deletion is
direct.
If you are using factory_girl you can add trait :with_users like this:
FactoryGirl.define do
factory :client do
trait :with_two_users do
after(:create) do |client|
client.users = create_list :user, 2
end
end
end
end
Now you can create a client with users in test like this:
client = create :client, :with_two_users
accepts_nested_attributes_for :users
and do as so:
Client.create!(name: 'Client1', users_attributes: { ........ })
hope this would work for you.
You can make use of after_create callback
class Client < ApplicationRecord
has_many :client_users, dependent: :destroy
has_many :users, through: :client_users
after_create :add_users
private def add_users
sef.users << [User.first, User.second]
end
end
Alternatively, A simpler approach would be
Client.create!(name: 'Client1', user_ids: [User.first.id, User.second.id])
The reason you're getting a mismatch is because you're specifying the client_users association that expects ClientUser instances, but you're passing in User instances:
# this won't work
Client.create!(client_users: [User.first, User.second])
Instead, since you already specified a users association, you can do this:
Client.create!(users: [User.first, User.second])
There's a simpler way to handle this, though: ditch the join model and use a has_and_belongs_to_many relationship. You still need a clients_users join table in the database, but you don't need a ClientUser model. Rails will handle this automatically under the covers.
class Client < ApplicationRecord
has_and_belongs_to_many :users
end
class User
has_and_belongs_to_many :clients
end
# Any of these work:
client = Client.new(name: "Kung Fu")
user = client.users.new(name: "Panda")
client.users << User.new(name: "Nemo")
client.save # => this will create two users and a client, and add two records to the `clients_users` join table

ActiveRecord::HasManyThroughAssociationNotFoundError problem, with a twist

I have the following three models
LegacyRole:
class LegacyRole < LegacyModel
has_many :permissions_roles
has_many :permissions, :through => :permissions_roles
end
LegacyPermissionsRole:
class LegacyPermissionsRole < LegacyModel
belongs_to :role
belongs_to :permission
end
and LegacyPermission:
class LegacyPermission < LegacyModel
has_many :permissions_roles
has_many :roles, :through => :permissions_roles
end
And in order for these to all work, and connect the legacy database and whatnot, I have the following class LegacyModel which is possibly trying to be too clever...
require 'active_record'
class LegacyModel < ActiveRecord::Base
self.abstract_class = true
establish_connection "legacy_#{::Rails.env}"
def self.inherited(subclass)
tabeleized_name = subclass.name.tableize
raise "Legacy models must be prefixed with 'Legacy'" unless tabeleized_name.start_with?('legacy_')
logger.info "***********LOAD***********"
logger.info "Loaded legacy model: #{subclass.name} using table: #{tabeleized_name.gsub('legacy_', '')}"
super
subclass.set_table_name tabeleized_name.gsub('legacy_','')
end
# these methods do much the same thing, can probably abstract some of this out
def self.belongs_to(association_id, options = {})
new_association = association_id.to_s.insert(0, 'legacy_').to_sym
old_association = association_id
logger.info "Legacy model has belongs_to association: '#{association_id}'"
association_id = association_id.to_s.insert(0, 'legacy_').to_sym
logger.info "Converting association to: '#{association_id}'"
unless options.has_key?(:foreign_key)
# our foreign_key is missing
options[:foreign_key] = old_association.to_s.foreign_key
logger.info("Foreign_key was missing, is now: #{options[:foreign_key]}")
end
super
alias_method old_association, new_association
end
def self.has_many(association_id, options = {})
new_association = association_id.to_s.insert(0, 'legacy_').to_sym
old_association = association_id
logger.info "Legacy model has_many to association: '#{association_id}'"
association_id = association_id.to_s.insert(0, 'legacy_').to_sym
logger.info "Converting association to: '#{association_id}'"
logger.debug("Association options are: #{options.inspect}")
if options.has_key?(:through)
options[:through] = options[:through].to_s.insert(0, 'legacy_')
logger.info("Through mutated, is now: #{options[:through]}")
end
super
alias_method old_association, new_association
end
end
Whenever I try to access permissions on an instance of LegacyRole, I get the following Active Record error:
ActiveRecord::HasManyThroughAssociationNotFoundError: Could not find the association "legacy_permissions_roles" in model LegacyRole
I've stepped through all this as best I can and I really can't figure out why this is occurring, obviously with this being a bit more complicated than standard by the LegacyModel class I really don't know how to diagnose this further... I'm now at the point with it where I can't see the forest for the trees and feel it might just be something really simple that I've missed out!
Edit:
Here is the log output from the models loading
****************************
Loaded legacy model: LegacyPermission using table: permissions
Legacy model has_many association: 'permissions_roles'
Converting association to: 'legacy_permissions_roles'
Association options are: {}
Legacy model has_many association: 'roles'
Converting association to: 'legacy_roles'
Association options are: {:through=>:permissions_roles}
Changed :through to: 'legacy_permissions_roles'
****************************
Loaded legacy model: LegacyPermissionsRole using table: permissions_roles
Legacy model has belongs_to association: 'role'
Converting association to: 'legacy_role'
Legacy model has belongs_to association: 'permission'
Converting association to: 'legacy_permission'
Foreign_key was missing, is now: 'permission_id'
****************************
Loaded legacy model: LegacyRole using table: roles
Legacy model has_many association: 'permissions_roles'
Converting association to: 'legacy_permissions_roles'
Association options are: {}
Legacy model has_many association: 'permissions'
Converting association to: 'legacy_permissions'
Association options are: {:through=>:permissions_roles}
Changed :through to: 'legacy_permissions_roles'
Perhaps you want
class LegacyRole < LegacyModel
has_many :permissions_roles
has_many :permissions, :through => :legacy_permissions_roles # note the `legacy` prefix
end
Or was this a typo in your post?

Rails 3, Rspec/Cucumber, and Factory Girl: No method error on nested/associated objects

I've been bashing my head against a wall for a while on this one and I can't get it to work. I have three models:
class Instrument < ActiveRecord::Base
has_many :analytical_methods
has_many :analytes, :through => :analytical_methods
accepts_nested_attributes_for :analytical_methods
attr_accessible :name, :analytical_methods_attributes
end
class AnalyticalMethod < ActiveRecord::Base
belongs_to :instrument
has_many :analytes
accepts_nested_attributes_for :analytes
attr_accessible :name, :analytes_attributes
end
class Analyte < ActiveRecord::Base
belongs_to :analytical_method
attr_accessible :name
end
And I have the following factories:
Factory.define :analyte do |analyte|
analyte.name "Test analyte"
end
Factory.define :analytical_method do |analytical_method|
analytical_method.name "Test method"
analytical_method.association :analyte
end
Factory.define :instrument do |instrument|
instrument.name "Test instrument"
instrument.association :analytical_method
instrument.association :analyte
end
Any time I try to Factory(:instrument) or Factory(:analytical_method), it throws the following error:
NoMethodError:
undefined method `analyte=' for #<AnalyticalMethod:0x00000104c44758>
Am I missing some ridiculous typo or something? The website works perfectly fine, but the tests keep failing. Thanks for any help in returning my sanity!
I believe it's because you're using instrument.association :analyte and analytical_method.association :analyte for a has_many relationship. The association declaration is used for belongs_to relationships.
I typically don't use Factory Girl to create has_many relationships, but if you choose to go this route, you're not the first person to do so. Here's a blog post that's a few years old, but seems to describe what you're trying to do.

Resources