Hey campers,
I''m wondering if any of you know a better solution to
skylerrichter''s
problem: http://github.com/camping/camping/issues#issue/28
The basic idea is that he want to create a Company, and then the first
User in that Company:
@company = Company.create(
:name => @input.name,
:sub_domain => @input.subdomain)
# Create the first user:
@user = User.create(
:company_id => @company.id,
:first_name => @input.first_name,
:last_name => @input.last_name,
:email => @input.email,
:password => @input.password)
Both Company and User has validations, so there''s a possibility that
they don''t actually get saved to the DB, and in that case he
don''t want
*any* of them to be saved (I assume). I was thinking about something like this:
begin
Company.transaction do
@company = Company.create!(
:name => @input.name,
:sub_domain => @input.subdomain)
@user = User.create!(
:company_id => @company.id,
:first_name => @input.first_name,
:last_name => @input.last_name,
:email => @input.email,
:password => @input.password)
end
rescue
@errors = [@company, @user].compact.map(&:full_messages).flatten
render :errors
else
redirect Login
end
But I''m wondering if there''s a better way to solve this?
// Magnus Holm
You could check if both the company and user are valid, and if so create them. @company = Company.new (...) @user = User.new (...) if (@company.valid? and @user.valid?) @company.save @user.save ) Dave On Sat, Jul 31, 2010 at 7:20 AM, Magnus Holm <judofyr at gmail.com> wrote:> Hey campers, > > I''m wondering if any of you know a better solution to skylerrichter''s > problem: http://github.com/camping/camping/issues#issue/28 > > The basic idea is that he want to create a Company, and then the first > User in that Company: > > ? @company = Company.create( > ? ? :name => @input.name, > ? ? :sub_domain => @input.subdomain) > > ? # Create the first user: > ? @user = User.create( > ? ? :company_id => @company.id, > ? ? :first_name => @input.first_name, > ? ? :last_name => @input.last_name, > ? ? :email => @input.email, > ? ? :password => @input.password) > > Both Company and User has validations, so there''s a possibility that > they don''t actually get saved to the DB, and in that case he don''t want > *any* of them to be saved (I assume). I was thinking about something like this: > > ? begin > ? ? Company.transaction do > ? ? ? @company = Company.create!( > ? ? ? ? :name => @input.name, > ? ? ? ? :sub_domain => @input.subdomain) > > ? ? ? @user = User.create!( > ? ? ? ? :company_id => @company.id, > ? ? ? ? :first_name => @input.first_name, > ? ? ? ? :last_name => @input.last_name, > ? ? ? ? :email => @input.email, > ? ? ? ? :password => @input.password) > ? ? end > ? rescue > ? ? @errors = [@company, @user].compact.map(&:full_messages).flatten > ? ? render :errors > ? else > ? ? redirect Login > ? end > > But I''m wondering if there''s a better way to solve this? > > // Magnus Holm > _______________________________________________ > Camping-list mailing list > Camping-list at rubyforge.org > http://rubyforge.org/mailman/listinfo/camping-list >-- Dave
@David Susco I figured that was the way to do it. Thats what I tried the first time but I seem to only be able to validate 1 item at a time. It only validates the company model and it ignores the "&& @user.valid?" If I rearrange my code so that the user gets saved first then only the user validates and then it ignores the "&& @company.valid?". Any ideas? On Sat, Jul 31, 2010 at 6:31 AM, David Susco <dsusco at gmail.com> wrote:> You could check if both the company and user are valid, and if so create them. > > @company = Company.new (...) > @user = User.new (...) > > if (@company.valid? and @user.valid?) > ?@company.save > ?@user.save > ) > > Dave > > On Sat, Jul 31, 2010 at 7:20 AM, Magnus Holm <judofyr at gmail.com> wrote: >> Hey campers, >> >> I''m wondering if any of you know a better solution to skylerrichter''s >> problem: http://github.com/camping/camping/issues#issue/28 >> >> The basic idea is that he want to create a Company, and then the first >> User in that Company: >> >> ? @company = Company.create( >> ? ? :name => @input.name, >> ? ? :sub_domain => @input.subdomain) >> >> ? # Create the first user: >> ? @user = User.create( >> ? ? :company_id => @company.id, >> ? ? :first_name => @input.first_name, >> ? ? :last_name => @input.last_name, >> ? ? :email => @input.email, >> ? ? :password => @input.password) >> >> Both Company and User has validations, so there''s a possibility that >> they don''t actually get saved to the DB, and in that case he don''t want >> *any* of them to be saved (I assume). I was thinking about something like this: >> >> ? begin >> ? ? Company.transaction do >> ? ? ? @company = Company.create!( >> ? ? ? ? :name => @input.name, >> ? ? ? ? :sub_domain => @input.subdomain) >> >> ? ? ? @user = User.create!( >> ? ? ? ? :company_id => @company.id, >> ? ? ? ? :first_name => @input.first_name, >> ? ? ? ? :last_name => @input.last_name, >> ? ? ? ? :email => @input.email, >> ? ? ? ? :password => @input.password) >> ? ? end >> ? rescue >> ? ? @errors = [@company, @user].compact.map(&:full_messages).flatten >> ? ? render :errors >> ? else >> ? ? redirect Login >> ? end >> >> But I''m wondering if there''s a better way to solve this? >> >> // Magnus Holm >> _______________________________________________ >> Camping-list mailing list >> Camping-list at rubyforge.org >> http://rubyforge.org/mailman/listinfo/camping-list >> > > > > -- > Dave > _______________________________________________ > Camping-list mailing list > Camping-list at rubyforge.org > http://rubyforge.org/mailman/listinfo/camping-list >
That''s weird, I can''t test anything until Monday but what
happens when
you nest it in two ifs?
If @company.valid?
if @user.valid?
save
On Sat, Jul 31, 2010 at 12:17 PM, Skyler Richter
<skylerrichter at gmail.com> wrote:> @David Susco
>
> I figured that was the way to do it. Thats what I tried the first time
> but I seem to only be able to validate 1 item at a time. It only
> validates the company model and it ignores the "&&
@user.valid?" If I
> rearrange my code so that the user gets saved first then only the user
> validates and then it ignores the "&& @company.valid?".
Any ideas?
>
> On Sat, Jul 31, 2010 at 6:31 AM, David Susco <dsusco at gmail.com>
wrote:
>> You could check if both the company and user are valid, and if so
create them.
>>
>> @company = Company.new (...)
>> @user = User.new (...)
>>
>> if (@company.valid? and @user.valid?)
>> ?@company.save
>> ?@user.save
>> )
>>
>> Dave
>>
>> On Sat, Jul 31, 2010 at 7:20 AM, Magnus Holm <judofyr at
gmail.com> wrote:
>>> Hey campers,
>>>
>>> I''m wondering if any of you know a better solution to
skylerrichter''s
>>> problem: http://github.com/camping/camping/issues#issue/28
>>>
>>> The basic idea is that he want to create a Company, and then the
first
>>> User in that Company:
>>>
>>> ? @company = Company.create(
>>> ? ? :name => @input.name,
>>> ? ? :sub_domain => @input.subdomain)
>>>
>>> ? # Create the first user:
>>> ? @user = User.create(
>>> ? ? :company_id => @company.id,
>>> ? ? :first_name => @input.first_name,
>>> ? ? :last_name => @input.last_name,
>>> ? ? :email => @input.email,
>>> ? ? :password => @input.password)
>>>
>>> Both Company and User has validations, so there''s a
possibility that
>>> they don''t actually get saved to the DB, and in that case
he don''t want
>>> *any* of them to be saved (I assume). I was thinking about
something like this:
>>>
>>> ? begin
>>> ? ? Company.transaction do
>>> ? ? ? @company = Company.create!(
>>> ? ? ? ? :name => @input.name,
>>> ? ? ? ? :sub_domain => @input.subdomain)
>>>
>>> ? ? ? @user = User.create!(
>>> ? ? ? ? :company_id => @company.id,
>>> ? ? ? ? :first_name => @input.first_name,
>>> ? ? ? ? :last_name => @input.last_name,
>>> ? ? ? ? :email => @input.email,
>>> ? ? ? ? :password => @input.password)
>>> ? ? end
>>> ? rescue
>>> ? ? @errors = [@company,
@user].compact.map(&:full_messages).flatten
>>> ? ? render :errors
>>> ? else
>>> ? ? redirect Login
>>> ? end
>>>
>>> But I''m wondering if there''s a better way to
solve this?
>>>
>>> // Magnus Holm
>>> _______________________________________________
>>> Camping-list mailing list
>>> Camping-list at rubyforge.org
>>> http://rubyforge.org/mailman/listinfo/camping-list
>>>
>>
>>
>>
>> --
>> Dave
>> _______________________________________________
>> Camping-list mailing list
>> Camping-list at rubyforge.org
>> http://rubyforge.org/mailman/listinfo/camping-list
>>
> _______________________________________________
> Camping-list mailing list
> Camping-list at rubyforge.org
> http://rubyforge.org/mailman/listinfo/camping-list
>
--
Dave
I had this problem in Rails! Yes, the short circuit evaluation messes
it up. So I did this:
if [@company.valid?, @user.valid?].all?
# do stuff
end
jeremy
On Sat, Jul 31, 2010 at 9:43 PM, David Susco <dsusco at gmail.com>
wrote:> That''s weird, I can''t test anything until Monday but what
happens when
> you nest it in two ifs?
>
> If @company.valid?
> ?if @user.valid?
> ? ?save
>
> On Sat, Jul 31, 2010 at 12:17 PM, Skyler Richter
> <skylerrichter at gmail.com> wrote:
>> @David Susco
>>
>> I figured that was the way to do it. Thats what I tried the first time
>> but I seem to only be able to validate 1 item at a time. It only
>> validates the company model and it ignores the "&&
@user.valid?" If I
>> rearrange my code so that the user gets saved first then only the user
>> validates and then it ignores the "&&
@company.valid?". Any ideas?
>>
>> On Sat, Jul 31, 2010 at 6:31 AM, David Susco <dsusco at
gmail.com> wrote:
>>> You could check if both the company and user are valid, and if so
create them.
>>>
>>> @company = Company.new (...)
>>> @user = User.new (...)
>>>
>>> if (@company.valid? and @user.valid?)
>>> ?@company.save
>>> ?@user.save
>>> )
>>>
>>> Dave
>>>
>>> On Sat, Jul 31, 2010 at 7:20 AM, Magnus Holm <judofyr at
gmail.com> wrote:
>>>> Hey campers,
>>>>
>>>> I''m wondering if any of you know a better solution to
skylerrichter''s
>>>> problem: http://github.com/camping/camping/issues#issue/28
>>>>
>>>> The basic idea is that he want to create a Company, and then
the first
>>>> User in that Company:
>>>>
>>>> ? @company = Company.create(
>>>> ? ? :name => @input.name,
>>>> ? ? :sub_domain => @input.subdomain)
>>>>
>>>> ? # Create the first user:
>>>> ? @user = User.create(
>>>> ? ? :company_id => @company.id,
>>>> ? ? :first_name => @input.first_name,
>>>> ? ? :last_name => @input.last_name,
>>>> ? ? :email => @input.email,
>>>> ? ? :password => @input.password)
>>>>
>>>> Both Company and User has validations, so there''s a
possibility that
>>>> they don''t actually get saved to the DB, and in that
case he don''t want
>>>> *any* of them to be saved (I assume). I was thinking about
something like this:
>>>>
>>>> ? begin
>>>> ? ? Company.transaction do
>>>> ? ? ? @company = Company.create!(
>>>> ? ? ? ? :name => @input.name,
>>>> ? ? ? ? :sub_domain => @input.subdomain)
>>>>
>>>> ? ? ? @user = User.create!(
>>>> ? ? ? ? :company_id => @company.id,
>>>> ? ? ? ? :first_name => @input.first_name,
>>>> ? ? ? ? :last_name => @input.last_name,
>>>> ? ? ? ? :email => @input.email,
>>>> ? ? ? ? :password => @input.password)
>>>> ? ? end
>>>> ? rescue
>>>> ? ? @errors = [@company,
@user].compact.map(&:full_messages).flatten
>>>> ? ? render :errors
>>>> ? else
>>>> ? ? redirect Login
>>>> ? end
>>>>
>>>> But I''m wondering if there''s a better way to
solve this?
>>>>
>>>> // Magnus Holm
>>>> _______________________________________________
>>>> Camping-list mailing list
>>>> Camping-list at rubyforge.org
>>>> http://rubyforge.org/mailman/listinfo/camping-list
>>>>
>>>
>>>
>>>
>>> --
>>> Dave
>>> _______________________________________________
>>> Camping-list mailing list
>>> Camping-list at rubyforge.org
>>> http://rubyforge.org/mailman/listinfo/camping-list
>>>
>> _______________________________________________
>> Camping-list mailing list
>> Camping-list at rubyforge.org
>> http://rubyforge.org/mailman/listinfo/camping-list
>>
>
>
>
> --
> Dave
> _______________________________________________
> Camping-list mailing list
> Camping-list at rubyforge.org
> http://rubyforge.org/mailman/listinfo/camping-list
>