Forum: Ruby on Rails SPECIALS RELATIONSHIP!?

027f88d17068dde230d0261d70bea262?d=identicon&s=25 Simon Eric (noumedem)
on 2014-05-28 15:30
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?
8d85d54dfa0b9c3d3a8f2774d7610713?d=identicon&s=25 Jesse Knutsen (Guest)
on 2014-05-28 15:48
(Received via mailing list)
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.
4c6bde00168d595053c09aac7e487f8e?d=identicon&s=25 Colin Law (Guest)
on 2014-05-28 15:48
(Received via mailing list)
On 28 May 2014 14:30, Simon Eric <lists@ruby-forum.com> wrote:
> 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
027f88d17068dde230d0261d70bea262?d=identicon&s=25 Simon Eric (noumedem)
on 2014-05-28 16:53
Thank you very much Jesse!
027f88d17068dde230d0261d70bea262?d=identicon&s=25 Simon Eric (noumedem)
on 2014-05-28 16:54
Thank you very much Colin. I will try that :)
027f88d17068dde230d0261d70bea262?d=identicon&s=25 Simon Eric (noumedem)
on 2014-05-28 18:22
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
4c6bde00168d595053c09aac7e487f8e?d=identicon&s=25 Colin Law (Guest)
on 2014-05-28 18:30
(Received via mailing list)
On 28 May 2014 17:22, Simon Eric <lists@ruby-forum.com> wrote:
> 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 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
027f88d17068dde230d0261d70bea262?d=identicon&s=25 Simon Eric (noumedem)
on 2014-05-28 19:00
Please i try this:
@postis.update_column(stato: "checked")
 def posti_multiple
    @postis = Posti.find(params[:posti_ids])


       @postis.update_column(stato: "checked")


   end
undefined method `update_column' for #<Array:0xb7a50e8>
4c6bde00168d595053c09aac7e487f8e?d=identicon&s=25 Colin Law (Guest)
on 2014-05-28 20:27
(Received via mailing list)
On 28 May 2014 18:00, Simon Eric <lists@ruby-forum.com> wrote:
> 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
027f88d17068dde230d0261d70bea262?d=identicon&s=25 Simon Eric (noumedem)
on 2014-05-28 23:35
@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??
4c6bde00168d595053c09aac7e487f8e?d=identicon&s=25 Colin Law (Guest)
on 2014-05-29 22:57
(Received via mailing list)
On 28 May 2014 22:35, Simon Eric <lists@ruby-forum.com> wrote:
> @postis is an string.

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
027f88d17068dde230d0261d70bea262?d=identicon&s=25 Simon Eric (noumedem)
on 2014-05-30 10:09
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 :)
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.