has_many through question

hi there, i thing i just have a logical problem.

i have following models.

class Area < ActiveRecord::Base   belongs_to :taskschedule   belongs_to :service end

class Service < ActiveRecord::Base   has_many :taskschedules, :through => :areas   has_many :areas end

class Taskschedule < ActiveRecord::Base   has_many :services, :through => :areas   has_many :areas end

Following View

Taskschedule#new <h2>New Taskschedule</h2>

<%= form_for @taskschedule, :url=>taskschedules_path do |f| %>   <p>Area: <%= f.text_field :name %></p>   <% for service in Service.find(:all) %>     <p><%= check_box_tag :service_ids, service.id, @taskschedule.services.include?(service), :name => 'taskschedule[service_ids]' -%> <%= service.name %></p>

  <% end %>   <%= submit_tag "Submit" %> <% end -%>

Taskschedule Controller class TaskschedulesController < ApplicationController   def index     @taskschedules = Taskschedule.find(:all)   end

  def new     @taskschedule = Taskschedule.new     @taskschedule.build.build_area   end

  def create     @taskschedule = Taskschedule.new(params[:taskschedule])     if @taskschedule.save       redirect_to taskschedules_path     end   end

  def edit     @taskschedule = Taskschedule.find(params[:id])   end

  def update     @taskschedule = Taskschedule.find(params[:id])     if @taskschedule.update_attributes(params[:taskschedule])       redirect_to taskschedules_path     else       render 'edit'     end   end

end

i have a text_field in areas that i want to be able to use in the taskschedule#new view? Thanks for any help. ed

hi there,

i thing i just have a logical problem.

i have following models.

class Area < ActiveRecord::Base

    belongs_to :taskschedule

    belongs_to :service

end

class Service < ActiveRecord::Base

    has_many :taskschedules, :through => :areas

    has_many :areas

end

class Taskschedule < ActiveRecord::Base

    has_many :services, :through => :areas

    has_many :areas

end

Following View

Taskschedule#new

New Taskschedule

<%= form_for @taskschedule, :url=>taskschedules_path do |f| %>

    <p>Area: <%= f.text_field :name %></p>

    <% for service in Service.find(:all) %>

            <p><%= check_box_tag :service_ids, [service.id](http://service.id),

@taskschedule.services.include?(service), :name =>

‘taskschedule[service_ids]’ -%> <%= service.name %>

    <% end %>

    <%= submit_tag "Submit" %>

<% end -%>

Taskschedule Controller

class TaskschedulesController < ApplicationController

    def index

            @taskschedules = Taskschedule.find(:all)

    end



    def new

            @taskschedule = Taskschedule.new

            @taskschedule.build.build_area

    end



    def create

            @taskschedule = Taskschedule.new(params[:taskschedule])

            if @taskschedule.save

                    redirect_to taskschedules_path

            end

    end



    def edit

            @taskschedule = Taskschedule.find(params[:id])

    end



    def update

            @taskschedule = Taskschedule.find(params[:id])

            if @taskschedule.update_attributes(params[:taskschedule])

                    redirect_to taskschedules_path

            else

                    render 'edit'

            end

    end

end

i have a text_field in areas that i want to be able to use in the

taskschedule#new view?

The syntax of your question looks like a statement. What is your question? You are making whoever reads your question have to put thought into what you mean to ask.

i understand. i have 3 models. taskschedule, services and areas. each taskschedule has many services through the model area.

when i add(create/update) services to taskschedule i want to enter also some information to the model area. hope that makes sence. thanks ed

i understand. i have 3 models. taskschedule, services and areas. each taskschedule has many services through the model area.

when i add(create/update) services to taskschedule i want to enter also some information to the model area. hope that makes sence. thanks ed

So what are you stuck on? What problem are you looking to get an answer for?

B.

here is the taskschedule#new view

<%= form_for @taskschedule, :url=>taskschedules_path do |f| %>   <p>Area: <%= f.text_field :name %></p>   <% for service in Service.find(:all) %>     <p><%= check_box_tag :service_ids, service.id, @taskschedule.services.include?(service), :name => 'taskschedule[service_ids]' -%> <%= service.name %></p>

  <% end %>   <%= submit_tag "Submit" %> <% end -%>

as you can see i am assigning services to the taskschedule through the model "area". that works. the problem is i have a textfield in the model area that i want to fill over this view. i tried fields_for(:area) but get an error message? thanks, ed

here is the taskschedule#new view

<%= form_for @taskschedule, :url=>taskschedules_path do |f| %>

    <p>Area: <%= f.text_field :name %></p>

    <% for service in Service.find(:all) %>

            <p><%= check_box_tag :service_ids, [service.id](http://service.id),

@taskschedule.services.include?(service), :name =>

‘taskschedule[service_ids]’ -%> <%= service.name %>

Ok, I think I understand what you are doing. I think what you should look into is using nested forms, meaning where you can have forms for multiple models and have rails handle all this automatically… if I am understanding, once you post this you have some work to do on the controller side to save your data, right? There are two railscasts (at least) where you could start:

http://railscasts.com/episodes/196-nested-model-form-part-1 http://railscasts.com/episodes/197-nested-model-form-part-2