Hi I really need some help. I'm new on rails and I need to join three
tables in one view.

I have three models
class TypeEvent < ActiveRecord::Base
end
class Event < ActiveRecord::Base
belongs_to :type_event
belongs_to :computer
end

class Computer < ActiveRecord::Base
belongs_to :room
has_many :events, :dependent => :delete_all
end
The show action in the Computer controller is written like this :
def show
@computer = Computer.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @computer }
end
end
I would like to display the following fields in the show.html.erb view
type_events.name from the type_events table
events.information from the type_events table
events.state , events.created_at , envents.updated_at from the events
table

Can anybody help me ?

--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Search Discussions

  • Colin Law at Feb 7, 2012 at 12:01 pm

    On 7 February 2012 11:41, Tony N. wrote:
    Hi I really need some help. I'm new on rails and I need to join three
    tables in one view.

    I have three models
    class TypeEvent < ActiveRecord::Base
    You want has_many :events here.
    end
    class Event < ActiveRecord::Base
    belongs_to :type_event
    belongs_to :computer
    end

    class Computer < ActiveRecord::Base
    belongs_to :room
    has_many :events, :dependent => :delete_all
    end
    The show action in the Computer controller is written like this :
    def show
    @computer = Computer.find(params[:id])
    respond_to do |format|
    format.html # show.html.erb
    format.xml  { render :xml => @computer }
    end
    end
    I would like to display the following fields in the show.html.erb view
    type_events.name from the type_events table
    events.information from the type_events table
    events.state , events.created_at , envents.updated_at from the events
    table
    @computer.events will give you all the events for that computer so you
    can loop through those. Something like
    @computer.events.each do |event|
    then you can display the attributes for each event. The type_event
    for the event is then event.type_event so you can access its
    attributes also.

    Colin

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Tony N. at Feb 7, 2012 at 2:23 pm
    Thanks I'll try it right now. Thank you for your help.

    --
    Posted via http://www.ruby-forum.com/.

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Gerbdla at Feb 7, 2012 at 8:24 pm
    and I would name the class EventType for me it seems more logical and
    easier to read but that is just me.

    On Feb 7, 6:22 am, "Tony N." wrote:
    Thanks I'll try it right now. Thank you for your help.

    --
    Posted viahttp://www.ruby-forum.com/.
    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedFeb 7, '12 at 11:42a
activeFeb 7, '12 at 8:24p
posts4
users3
websiterubyonrails.org
irc#RubyOnRails

3 users in discussion

Tony N.: 2 posts Colin Law: 1 post Gerbdla: 1 post

People

Translate

site design / logo © 2022 Grokbase