0

Im building a report system which uses a sort of meta question model. Questions are previusly saved in the database, and then depending of the type of report some questions are taken from the database.

Wanting to keep things DRY, i'm trying to figure out a way to pass the information of the Variable model to my report_header with no avail.

In the new action i have:

  reportBody = @report_head.report_bodies.build(:variable_id => a.id)
  @report_head.variables #modified, thx.

all i need is to pass the attributes from the Variable to report_head in a DRY way.

If you need to know my models:

class Variable < ActiveRecord::Base
  attr_accessible :id,:break_point, :description, :name, :time_frequency, :v_type
  has_many :report_bodies
  has_many :report_heads, :through => :report_bodies   
end

class ReportHead < ActiveRecord::Base
  attr_accessible :email, :name , :report_bodies_attributes, :report_bodies, :variables_attributes
  has_many :report_bodies
  has_many :variables, :through => :report_bodies   
  accepts_nested_attributes_for :report_bodies
end

class ReportBody < ActiveRecord::Base
  attr_accessible :report_head_id, :variable_value, :variable_id, :variables_attributes, :report_heads
  belongs_to :report_head
  belongs_to :variable
end

Update

I updated the model as suggested, and modified the way to call the variables. However im still confused about how to use it in the view, if i do something like:

   <%= f.fields_for :variables do |variable| %>
       <%= variable.text_field :name, :value => :name, :class => 'text_field' %>  
   <% end %>

it prints a string instead of the actual name.

0

1 Answer 1

1

You have define wrong name association, your association of ReportBody should be:

belongs_to :report_head 
belongs_to :variable 

This is not correct:

@report_head.report_bodies.build(:variable_id => a.id,:report_head_id =>@report_head.id) 

chang it to:

@report_head.variables.build(:variable_id => a.id)

it's better, you don't have to set report_head_id. And this is wrong:

@report_head.report_bodies.variables

If you want to get all variables belong to @report_head, you just need using:

@report_head.variables
Sign up to request clarification or add additional context in comments.

5 Comments

no worries i will, you think you can hopefully help me on that last part?
is your form for create new object? fields_for only used for form create new object, you just need <%= variable.text_field :name %>
yes, my form is for creating reports, however my variables already exist. doing just that gives me the error undefined local variable or method 'variable' for #<#<Class:0xaea4fd4>:0xb4b21d8>
by the way, i used the console to test the output of @report_head.variables after doing @report_head.report_bodies.build(:variable_id => 1) however it returned an empty object
no, as my answer, you cannot use @report_head.report_bodies.build(:variable_id => 1), just @report_head.variables.build(name: 'something') to create new variable and insert to ReportBody.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.