.find() with simple :join

Hello all,

I about to pull my hair out.

I have two models “Activities” and “Activity_types”

Activities

id
name
activity_type_id

Activity_types

id
name

  • Associations -

class Activity < ActiveRecord::Base
has_many :activity_types
end

class ActivityType < ActiveRecord::Base
belongs_to :activity
end

  • activities_controller -

def index
@activities = Activity.find(:all, :joins => :activity_types)

respond_to do |format|
  format.html # index.html.erb
  format.xml  { render :xml => @activities }
end

end

  • index.html.erb -

Listing activities

<% @activityTypeTitle.each do |title| %>

<% @budgetActivities.each do |activity| %>

<% end %> <% end %>

<%= title.name %>

Name Action
<%=h activity.name %> <%= link_to 'Show', activity %> <%= link_to 'Edit', edit_activity_path(activity) %> <%= link_to 'Destroy', activity, :confirm => 'Are you sure?', :method => :delete %>
<%= link_to "New Activity", new_activity_path %>

All I’m trying to do is list the ‘types’ as a header of a table with the
activities in rows.

When I try to do the join, I get…

“SQLite3::SQLException: no such column: activity_types.activity_id:
SELECT “activities”.* FROM “activities” INNER JOIN “activity_types” ON
activity_types.activity_id = activities.id”

Thank you for any help with this.

JohnM

You’ve got your has_many and belongs_to backwards.

Thanks Mat,
That did it.
I checked with debug and how do I get the type Name?

John

if I include “:include => :activity_type”, then I get the name.

debug @activities

  • !ruby/object:Activity
    activity_type: &id001 !ruby/object:ActivityType
    attributes:
    name: Budgeting
    created_at: 2010-01-25 14:38:25
    updated_at: 2010-01-25 14:38:25
    id: “1”
    attributes_cache: {}

    attributes:
    name: Study Mapping & Budget Workup
    created_at: 2010-01-25 14:44:51
    updated_at: 2010-01-25 14:44:51
    activity_type_id: “1”
    id: “1”
    attributes_cache: {}

How to display “name”?

John

Thanks for help.

John

On Thu, Feb 11, 2010 at 11:56, John M. [email protected] wrote:

To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [email protected]
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

You should probably familiarize yourself with this:
http://guides.rubyonrails.org/association_basics.html

activity.activitytypes.name

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs