Allow inclusion of models associations as columns #6

Closed
brendon opened this Issue Aug 17, 2012 · 6 comments

2 participants

@brendon

Hi there, this seems like a pretty likely scenario:

csv.headers do |csv| 
  csv.columns :state, :building_name

end

csv.rows @warranties do |csv, warranty|
  csv.cells :state

  if warranty.building
    csv.cell :building_name, warranty.building.name
  end
end

At the moment I get a undefined methodbuilding_name' for #Warranty:0x007fbc0890ece8` error.

I guess it's because the code is calling the building_name method on the warranty even though we're not going to be using that method to get the actual value?

@brendon

Ah! figured it out upon examining your code:

if @model && value.nil?
   @cells[column] = @model.send(column)
else
   @cells[column] = value
end

In my case sometimes the value is nil because the attribute name on the building is nil. I guess in this special case I should be checking for nil and substituting an empty string?

Not sure if the solution lies in tightening up the code on your end or not?

@brendon

Maybe instead of passing the value in as a parameter, you could allow for the value to be calculated in a block on the method? That way you can determine whether one should fetch the value from the model using the attribute name, or whether the user explicitly wants to calculate the value that could include it coming out as nil.

@paulspringett

Hi @brendon your call to

csv.cell :building_name, warranty.building.name

shouldn't raise that exception. (There's a bug there, basically). I'll take a look ASAP.

@paulspringett

@brendon has been fixed in #8 - will update the gem this weekend.

@brendon
@paulspringett

@brendon see my response here: #8

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment