AS enforcing has_one when it should be has_many ???

15 views
Skip to first unread message

Kyle

unread,
Nov 26, 2011, 8:26:31 PM11/26/11
to ActiveScaffold : Ruby on Rails plugin
This is a terribly basic question around has_many/belongs_to
relationships and the "Add Existing" functionality that isn't working
as I'd expect.

Given:

class Program < ActiveRecord::Base
has_many :organizations
end

class Organization < ActiveRecord::Base
belongs_to :program
end

Use Case:

1. Create Program through AS called "Program 1" (verify successful)
2. Create Organization through AS called "Org 1", select "Program 1"
in select list and click "Add Existing" (verify Org 1 created
successfully)
3. Create another Organization through AS call "Org 2" selecting the
same Program as Org 1.

What AS does at that point is strange, it first goes and sets "Org 1"s
program to null, and then goes and creates "Org 2" with that program.
It's like AS thinks the relationship is has_one/belongs_to instead of
has_many/belongs_to. It won't allow multiple Org rows attached to the
same Program...which it darn well should.

What am I missing? (something simple I'm sure)

Kyle

unread,
Nov 26, 2011, 8:28:00 PM11/26/11
to ActiveScaffold : Ruby on Rails plugin
Should have mentioned this is using Rails 3.1.1 and active_scaffold
3.1.8

Atastor

unread,
Nov 28, 2011, 2:06:59 AM11/28/11
to actives...@googlegroups.com
Post the controllers, its something simple, I 'ld agree :-)


Sergio Cambra

unread,
Nov 28, 2011, 8:24:49 AM11/28/11
to actives...@googlegroups.com
On Sábado, 26 de noviembre de 2011 17:28:00 Kyle escribió:
> Should have mentioned this is using Rails 3.1.1 and active_scaffold
> 3.1.8

I have tried it with 3.1.10 and it's working. Can you tried updating
active_scaffold? Have you overrided some controller or helper methods, or some
views?

Kyle

unread,
Nov 28, 2011, 10:39:36 PM11/28/11
to ActiveScaffold : Ruby on Rails plugin
On Nov 28, 12:06 am, Atastor <mich...@portzblitz.de> wrote:
> Post the controllers, its something simple, I 'ld agree :-)

I haven't modified the controllers at all yet, just default
configuration on the AS controllers:

class OrganizationsController < ApplicationController
active_scaffold :organization do |conf|
end
end

class ProgramsController < ApplicationController
active_scaffold :program do |conf|
end
end

Kyle

unread,
Nov 28, 2011, 10:42:42 PM11/28/11
to ActiveScaffold : Ruby on Rails plugin

On Nov 28, 6:24 am, Sergio Cambra <ser...@entrecables.com> wrote:
> On Sábado, 26 de noviembre de 2011 17:28:00 Kyle escribió:
>
> > Should have mentioned this is using Rails 3.1.1 and active_scaffold
> > 3.1.8
>
> I have tried it with 3.1.10 and it's working. Can you tried updating
> active_scaffold?

Will do.

> Have you overrided some controller or helper methods, or some
> views?

Have not overridden anything, default controllers and helpers. All
I've done is generate scaffolds with the AS generator, fill in the
migrations, migrate, define models as previously shown. That's it.

It's been a couple years since I've used AS, but I would expect this
basic of a use case to still be the same and work with minimal effort.

Sergio Cambra

unread,
Nov 29, 2011, 3:59:15 AM11/29/11
to actives...@googlegroups.com

Yes, it should work, I have fixed the bug in master branch.

Kyle

unread,
Nov 29, 2011, 1:31:26 PM11/29/11
to ActiveScaffold : Ruby on Rails plugin
Updated active_scaffold gem to 3.1.10 and still seeing the same
problem. Here's the log traffic showing the issue when creating a new
org selecting the same parent Program:


Started POST "/organizations" for 127.0.0.1 at 2011-11-29 11:27:23
-0700
Processing by OrganizationsController#create as JS
Parameters: {"utf8"=>"✓", "authenticity_token"=>"f2SgrBTEMh9JupF
+jXiZtrSy77Rf0ifXk+EOM7PmEhk=", "record"=>{"name"=>"Org 3",
"email"=>"kyle....@gmail.com", "address_1"=>"", "address_2"=>"",
"city"=>"", "phone"=>"", "state"=>"", "url"=>"", "zip"=>"",
"program"=>{"name"=>"Demo", "description"=>"Demo program", "id"=>"1"},
"products"=>{"1322591204075"=>{"name"=>"", "description"=>""}}},
"associated_id"=>"1", "commit"=>"Create"}
Program Load (0.1ms) SELECT "programs".* FROM "programs" WHERE
"programs"."id" = ? LIMIT 1 [["id", "1"]]
Organization Load (0.2ms) SELECT "organizations".* FROM
"organizations" WHERE "organizations"."program_id" = 1

Here's where it sets all existing organization.program_id values to
NULL ...WHY??

SQL (0.5ms) UPDATE "organizations" SET "program_id" = NULL WHERE
"organizations"."program_id" = 1 AND "organizations"."id" IN (1)

After that now it creates the new org...

SQL (45.0ms) INSERT INTO "organizations" ("address_1", "address_2",
"city", "created_at", "email", "name", "phone", "program_id", "state",
"updated_at", "url", "zip") VALUES
(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) [["address_1", nil],
["address_2", nil], ["city", nil], ["created_at", Tue, 29 Nov 2011
18:27:23 UTC +00:00], ["email", "kyle....@gmail.com"], ["name",
"Org 3"], ["phone", nil], ["program_id", 1], ["state", nil],
["updated_at", Tue, 29 Nov 2011 18:27:23 UTC +00:00], ["url", nil],
["zip", nil]]

Atastor

unread,
Nov 30, 2011, 1:12:45 AM11/30/11
to actives...@googlegroups.com
Sergion didn't version bump this latest change, so you can't access it with gem 'action_scaffold' in Gemfile; you need


gem 'action_scaffold', :git => 'git://github.com/activescaffold/active_scaffold.git'

RM
 

Sergio Cambra

unread,
Nov 30, 2011, 3:34:40 AM11/30/11
to actives...@googlegroups.com
On Martes, 29 de noviembre de 2011 10:31:26 Kyle escribió:
> Updated active_scaffold gem to 3.1.10 and still seeing the same
> problem. Here's the log traffic showing the issue when creating a new
> org selecting the same parent Program:
>

It's fixed in master, not released yet. Use :git =>
'git://github.com/active_scaffold/active_scaffold.git' or wait until it's
released

Kyle

unread,
Dec 2, 2011, 1:30:33 AM12/2/11
to ActiveScaffold : Ruby on Rails plugin
Thanks! Using the master branch fixed it.
Appreciate the help

On Nov 30, 1:34 am, Sergio Cambra <ser...@entrecables.com> wrote:
> On Martes, 29 de noviembre de 2011 10:31:26 Kyle escribió:
>
> > Updated active_scaffold gem to 3.1.10 and still seeing the same
> > problem. Here's the log traffic showing the issue when creating a new
> > org selecting the same parent Program:
>
> It's fixed in master, not released yet. Use :git =>
> 'git://github.com/active_scaffold/active_scaffold.git' or wait until it's
> released
>
>
>
>
>
>
>
>
>
> > Started POST "/organizations" for 127.0.0.1 at 2011-11-29 11:27:23
> > -0700
> >   Processing by OrganizationsController#create as JS
> >   Parameters: {"utf8"=>"✓", "authenticity_token"=>"f2SgrBTEMh9JupF
> > +jXiZtrSy77Rf0ifXk+EOM7PmEhk=", "record"=>{"name"=>"Org 3",

> > "email"=>"kyle.cam...@gmail.com", "address_1"=>"", "address_2"=>"",


> > "city"=>"", "phone"=>"", "state"=>"", "url"=>"", "zip"=>"",
> > "program"=>{"name"=>"Demo", "description"=>"Demo program", "id"=>"1"},
> > "products"=>{"1322591204075"=>{"name"=>"", "description"=>""}}},
> > "associated_id"=>"1", "commit"=>"Create"}
> >   Program Load (0.1ms)  SELECT "programs".* FROM "programs" WHERE
> > "programs"."id" = ? LIMIT 1  [["id", "1"]]
> >   Organization Load (0.2ms)  SELECT "organizations".* FROM
> > "organizations" WHERE "organizations"."program_id" = 1
>
> > Here's where it sets all existing organization.program_id values to
> > NULL ...WHY??
>
> > SQL (0.5ms)  UPDATE "organizations" SET "program_id" = NULL WHERE
> > "organizations"."program_id" = 1 AND "organizations"."id" IN (1)
>
> > After that now it creates the new org...
>
> >   SQL (45.0ms)  INSERT INTO "organizations" ("address_1", "address_2",
> > "city", "created_at", "email", "name", "phone", "program_id", "state",
> > "updated_at", "url", "zip") VALUES
> > (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)  [["address_1", nil],
> > ["address_2", nil], ["city", nil], ["created_at", Tue, 29 Nov 2011

> > 18:27:23 UTC +00:00], ["email", "kyle.cam...@gmail.com"], ["name",

Reply all
Reply to author
Forward
0 new messages