Search code examples
mysqlruby-on-railsrubydatabasenomethoderror

not calling a table parameter in rails sql database


enter image description hereI'm a new ruby developer and cant find the error both the database has the table but it is not calling

<% @orders.each do |order| %>


    <tr>

        <td><%= order.listing.name %></td>
        <td><%= order.buyer.name %></td>
        <td><%= order.created_at.strftime("%B %-d, %Y") %></td>

    </tr>
    <% end %>
</table>  

and the database has a value added to the orders and listings-name.

Total GitHub repo is https://github.com/dinesh124/roughmart?files=1 The error is nomethod error order.listing.name cannot find name - in sales page after sign up


Solution

  • You have incorrect association between Order and Listing. Look at your schema.rb - there is t.string "listing_id" in orders table, but it should be integer. Change column type in migration

    def up
      change_column :orders, :listing_id, :integer
    end
    
    def down
      change_column :orders, :listing_id, :text
    end
    

    You need to use up and down methods since change_column is irreversible