I've found an hacky way to solve my problem:

in client class, I've added:
class Client
  include DataMapper::Resource

  property :id,         Serial
  property :prenom,     String
  property :nom,        String
  property :telephone,  String
  property :courriel,   String
  property :adresse,    Text
  property :view_only,  Boolean

  belongs_to :societe

  def societe_id
    societe.id
  end
end

and in the update method of class_controller, I've added:
class ClientsController < ApplicationController  # PUT /clients/1
  # PUT /clients/1.xml
  def update
    @client = Client.get(params[:id])
#########################
# <New Stuff>
    id = params[:client][:societe_id].to_i
    params[:client].delete(:societe_id)
#########################
# </New Stuff>

    respond_to do |format|
      if @client.update(params[:client])
#########################
# <New Stuff>
        if id != @client.societe.id
          @client.societe = nil
          new_societe = Societe.get(id)
          new_societe.clients << @client
          @client.save
        end
#########################
# </New Stuff>
        format.html { redirect_to(@client, :notice => 'Client was
successfully updated.') }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @client.errors, :status
=> :unprocessable_entity }
      end
    end
  end

end

It's ugly but it works...
I would like to open a ticket for dm-rails but I don't know how...

On 30 mar, 14:24, Sylvain <[email protected]> wrote:
> Hi all,
> I'm testing rails3 with Datamapper on a simple project.
>
> I've 2 classes :
> class Societe
>   include DataMapper::Resource
>
>   property :id,       Serial
>   property :nom,     String
>   property :adresse,  Text
>
>   has n, :clients
> end
>
> class Client
>   include DataMapper::Resource
>
>   property :id,         Serial
>   property :prenom,     String
>   property :nom,        String
>   property :telephone,  String
>   property :courriel,   String
>   property :adresse,    Text
>   property :view_only,  Boolean
>
>   belongs_to :societe
> end
>
> I created them using rails generate scaffold (except belongs/ has of
> course).
>
> I've then have in the folder  "view" 5 erb templates per class, and
> especially one (_form) used by "new" and "edit" templates.
> In order to select the society, I have added in client's _form a
> select:
>
> <%= form_for(@client) do |f| %>
>   <%= f.error_messages %>
> ...
> <div class="field">
>     <%= f.label :societe %><br />
>     <%= f.select(:societe_id, @societes.map{|s| [s.nom, s.id]}) %>
>   </div>
>   <div class="actions">
>     <%= f.submit %>
>   </div>
> <% end %>
>
> In creation mode, everything is allright and the customer is created
> with the link to its society
> But when I edit, I have the following error:
>
>  Showing /home/.../app/views/clients/_form.html.erb where line #30
> raised:
>
> undefined method `societe_id' for #<Client:0xa51833c>
>
> Extracted source (around line #30):
>
> 27:   </div>
> 28:   <div class="field">
> 29:     <%= f.label :societe %><br />
> 30:     <%= f.select(:societe_id, @societes.map{|s| [s.nom, s.id]}) %>
> 31:   </div>
> 32:   <div class="actions">
> 33:     <%= f.submit %>
>
> Any idea why it's working with empty "client" and not with existing
> one?
>
> Digging a little bit the problem, it seems it's because the select in
> base doesn't take the societe_id (which is "magically" retrieved I
> think):
>
> SELECT "id", "prenom", "nom", "telephone", "courriel", "view_only"
> FROM "clients" WHERE "id" = 1 ORDER BY "id" LIMIT 1
>
> Thanks in advance,
>
> Sylvain

-- 
You received this message because you are subscribed to the Google Groups 
"DataMapper" group.
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/datamapper?hl=en.

Reply via email to