Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7888777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:55:20+00:00 2026-06-03T05:55:20+00:00

Here are the model relationships: class Tool < ActiveRecord::Base … has_many :loss_ratios, :dependent =>

  • 0

Here are the model relationships:

class Tool < ActiveRecord::Base
  ...
  has_many :loss_ratios, :dependent => :destroy, :order => "loss_ratios.starts_at DESC"

  validates_associated :loss_ratios
  accepts_nested_attributes_for :loss_ratios, :allow_destroy => true

  attr_accessible :name, :text, :service_id, :supplier_id, :loss_ratios_attributes
end

class LossRatio < ActiveRecord::Base
  belongs_to :tool

  validates :rate, :starts_at, :tool, :presence => true
  validates_uniqueness_of :starts_at, :scope => :tool_id
  validates_numericality_of :rate
  validates_inclusion_of :rate, :in => (0..1)
  ...
end

I’m managing LossRatio associations in the create/update ToolsController actions. I want to test those by POSTing sets of attributes for a Tool (including a couple of nested LossRatios as though they were submitted in a form). I’m using FactoryGirl, but it doesn’t seem to have a way of constructing a params-like hash of attributes (attributes_for ignores associations, and looks like this behavior is not gonna change).
Is there a way to do this?

(I know the title is a mess, but I couldn’t think of anything better and shorter…)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T05:55:22+00:00Added an answer on June 3, 2026 at 5:55 am

    OK here’s what I’ve come up with after pulling my hair out for half a day:

    def params_for(factory_name)
      exclude_params = [ "id", "created_at", "updated_at" ]
      f = FactoryGirl.build(factory_name)
    
      params = f.attributes.except(*exclude_params).dup
    
      f.reflections.select { |k,v|
        v.macro == :has_many && !v.instance_of?(ActiveRecord::Reflection::ThroughReflection)
      }.each_key do |k|
        assoc_collection = f.send(k)
    
        unless assoc_collection.empty?
          params["#{k.to_s}_attributes"] = {}
    
          assoc_collection.each_with_index do |assoc_obj,idx|
            params["#{k.to_s}_attributes"][idx.to_s] = assoc_obj.attributes.except(*exclude_params << "#{f.class.name.underscore}_id")
          end
        end
      end
    
      params
    end
    

    This is a helper method used for building a params hash consumable by a controller’s CRUD actions. I’m using it in my controller specs like so:

    subject { post :create, :tool => params_for(:tool_with_lr_history) }
    
    it "creates a new tool" do
      expect { subject }.to change(Tool, :count).by(1)
    end
    

    As seen from the snippet, the method only populates attributes for has-many associations (and ignores has-many-through associations). I guess it might be extended for any kind of relationships, but that works for me so far (unless there’s a better way of doing what I want)…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's my user model: class User < ActiveRecord::Base devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable,
I am in rails 3.1. I have the following models class Tool < ActiveRecord::Base
Here is my model: class Company(models.Model): id = models.AutoField(primary_key=True); name = models.CharField(max_length=100); address =
Here's the model I implemented: public class LoginSession { private static final Gson gson
here is my model class gameUserTrophyInfo(models.Model): user = models.ForeignKey(userInfo) trophy = models.ForeignKey(gameTrophyInfo) date =
Here are the domain model classes: public abstract class BaseClass { ... } public
Here is a Django model I'm using. class Person(models.Model): surname = models.CharField(max_length=255, null=True, blank=True)
Here is the BlogPost model : @Entity @Table(name = blog_posts) public class BlogPost extends
Here is the model we are using to store the CC details how secure
I've got an interesting box-model problem here. I have a header full of links,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.