SPECIALS RELATIONSHIP!?

i want to associate one model with 4 models. For example i call a first model Spectacle and call others Hall1, Hall2, Hall3 and Hall4. All of Halls have the same attributes: id and seat. Each Hall have 10 seats. I want to create an spectacle on the database and associate it with any Hall. Problems: How can i do this association in rails? It is possible to do this type of association? How can i get all of Hall's seats through spectacle that i created?

does each hall have to be specifically designated as hallx? and do halls exist independently of spectacle?

Assuming that halls exist independently, it sounds like having a join table and position attributes on the join should work spectacle      has_many :halls

spectacle_hall_join      belongs_to :hall      belongs_to :spectacle

hall      has_many :spectacles, through: spectacle_hall_join

should do the trick

if you really need to have the ability to call hallx you could add a scope to the join      scope :position, lambda {|x| where position: x} # assumes code calls only, if users can supply a value you will want to prevent XSS on this

you can take this further to get exactly what you need.

Hope this helps.

i want to associate one model with 4 models. For example i call a first model Spectacle and call others Hall1, Hall2, Hall3 and Hall4. All of Halls have the same attributes: id and seat. Each Hall have 10 seats. I want to create an spectacle on the database and associate it with any Hall. Problems: How can i do this association in rails?

Just have one halls table, with an additional field of, for example, hall_number. Then have spectacle belongs_to hall, hall has_many spectacles (assuming this is what you want).

It is possible to do this type of association? How can i get all of Hall's seats through spectacle that i created?

To get all the seats for a spectacle, assuming that hall has_many seats use @spectacle.hall.seats

Colin

Thank you very much Jesse!

Thank you very much Colin. I will try that :slight_smile:

Colin i try what you tell me. It work find. Thank very much. The next step is that: if an user choice the seat on the hall, it must not be appear againt on the view.

I have some view like this:

<%= form_tag( riepilogo_path, method: "post", id: "sel") do %>

    <%= hidden_field_tag "sala", params[:sala] %>   <%= hidden_field_tag "spectacle_id", params[:spectacle_id] %>

  <%= hidden_field_tag "num", params[:num] %> <table>   <tr>     <th></th>     <th>Numero</th>

  </tr> <% for posti in @postis %>   <tr>     <td><%= check_box_tag "posti_ids", posti.id %></td>     <td><%=h posti.numero %></td>

  </tr> <% end %> </table> <%= submit_tag "OK", id: "sub"%> <% end %>

controller of Postis is something like so:

class PostisController < ApplicationController def index     @postis = Posti.where(:stato => "unchecked" , :spectacle_id => params[:spectacle_id] , :hall_num => params[:sala])   end

def posti_multiple     @postis = Posti.find(params[:posti_ids])

   end end

In the Posti's model i have attributes: spectacle_id, hall_num, seat(integer) and stato (:default => "unchecked").

An user choice her seat on the hall. When he submit the form, seats be load in posti_ids. I want to update stato of seats wich are present in posti_ids from "unchecked" to "checked" on the database. How can do that. Thank you very much

I guess you are a beginner with rails, if so I suggest that you work right through a good tutorial such as railstutorial.org (which is free to use online), including doing all the exercises, so that you will learn the basics of rails. Then you should be able to answer most of the basic questions yourself.

Colin

Please i try this: @postis.update_column(stato: "checked") but i have error like so: undefined method `update_column' for #<Array:0xb7a50e8>

What do you think the type of @postis is?

Colin

@postis is an string.

Now i see where i the problem. I fix them like so: def posti_multiple     @postis = Posti.find(params[:posti_ids])      a=Posti.find_by(numero: params[:posti_ids])        a.update_column(:stato, "checked")

end

When i have for example posti_ids[" any number"], one seat disappear on the view. When i have posti_ids["any number", "any number", ... ], one seat also desappear but i don't want that. All of seat wich are choice must be disappear. I think i can do some loop??

No it isn't, you had @postis = Posti.where(:stato => "unchecked" , :spectacle_id => params[:spectacle_id] , :hall_num => params[:sala]) so why would it be a string? The method 'where' returns (effectively) an array of records, hence the error you saw. So *please* just work right through a good tutorial. the time spent will be repaid many times over.

Colin

I resolved that problem.

I done somethig like so to update each column:

def update_multiple      @postis = Posti.find(params[:posti_ids])

     @postis.each do |posti|       posti.update_column(:stato, params[:posti])    end      redirect_to prenotazione_path(params[:spectacle_id])    end

<%= form_for :posti, :url => update_multiple_path, :html => { :method => :put } do |f| %> <%= hidden_field_tag "spectacle_id", params[:spectacle_id] %>   <strong>Posti scelti:</strong> <ul>   <% for posti in @postis %>   <li>      <%=h posti.numero %>

    <%= hidden_field_tag "posti_ids", posti.id %>   </li>   <% end %> </ul>

  <p>

    <%= f.hidden_field :stato, :value => "checked" %>   </p>   <p><%= f.submit "PRENOTA" %></p> <% end %>

It works very well and i am so very happy. Thank you Colin for helps. I will very well a tutorial when i will finish all my esams at university. Ruby on Rails is the best :slight_smile: