Ruby on Rails Wednesday, September 8, 2010

I've created a brand new Rails 2.3.8 app.
No gems, nothing.

I created only the bare necessities:
Two models (Member and Folder)
one controller (MembersController with only new and create actions)
one view (new).

I can create one member with default folders as per my callback (the
very first one), but any subsequent members don't get default folders
created for them.

But again, when I try the same thing from script/console everything
works (I create new member and, bam, two folders come into existence
as well)..

w t f....


On Sep 7, 3:31 pm, radhames brito <rbri...@gmail.com> wrote:
> check your attri_accesible, and check if you rae using a gem that uses
> attr_protected, it appears you have a problem with mass assignment and you
> cant see from one controller the attribute that member_id from the folder
> controller i think you need to be able to , also note that with before save
> there is no member_id in the database to compare the validation with thats
> why i said you should use a transaction and create everything from the
> member controller like this
>
> Member.transaction do
> @member.new(blah, blah)
> @member.save!
>  params[:folder_name].nil? ? name= "Default" : name = params[:folder_name]
> @folder = Folder.find(:conditions =>{:name = name, :member_id=> @member.id})
>
> @folder ||= Folder.new(blah blah)
> @folder.member = @member
> @folder.save!
> end
>
> try that, the code inside the transaction will rollback if either to the
> save fail and everything will go back to the its original state.
>
> On Tue, Sep 7, 2010 at 2:09 PM, Swartz <netv...@gmail.com> wrote:
> > Also note that I have to set at least one other folder attribute while
> > creating a folder ,
> > so this variant doesn't help me much.
>
> > On Sep 7, 8:33 am, radhames brito <rbri...@gmail.com> wrote:
> > > dont use the before_save use transactions that way it will roll back both
> > > actions if there is an error.
> > > and dont use that function you have with the each and all use
>
> > > find_or_create_by_name(name)
>
> > > it does what you want.
>
> > > On Tue, Sep 7, 2010 at 7:20 AM, Frederick Cheung <
> > frederick.che...@gmail.com
>
> > > > wrote:
>
> > > > On Sep 7, 11:55 am, Swartz <netv...@gmail.com> wrote:
>
> > > > > However, as soon as I create a member through a controller-backed
> > > > > form, the uniqueness validation gets tripped up.  I get validation
> > > > > error from my validates_uniqueness_of as "Folder already exists for
> > > > > this member". Which shouldn't happen.
>
> > > > > For some strange reason :scope gets ignored and thus uniqueness check
> > > > > isn't properly scoped by member_id...
>
> > > > > The weird part is that it all works in script/console, but not via
> > web
> > > > > forms.
>
> > > > Is there some odd interaction because you're creating these folders
> > > > from a before_save  (ie the member's id has not yet been set)?
>
> > > > Fred
>
> > > > > Any suggestions???
>
> > > > > Thanks in advance!
>
> > > > --
> > > > You received this message because you are subscribed to the Google
> > Groups
> > > > "Ruby on Rails: Talk" group.
> > > > To post to this group, send email to rubyonrails-talk@googlegroups.com
> > .
> > > > To unsubscribe from this group, send email to
> > > > rubyonrails-talk+unsubscribe@googlegroups.com<rubyonrails-talk%2Bunsubscribe@googlegroups.com>
> > <rubyonrails-talk%2Bunsubscribe@googlegroups.com<rubyonrails-talk%252Bunsubscribe@googlegroups.com>
>
> > > > .
> > > > For more options, visit this group at
> > > >http://groups.google.com/group/rubyonrails-talk?hl=en.
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Ruby on Rails: Talk" group.
> > To post to this group, send email to rubyonrails-talk@googlegroups.com.
> > To unsubscribe from this group, send email to
> > rubyonrails-talk+unsubscribe@googlegroups.com<rubyonrails-talk%2Bunsubscribe@googlegroups.com>
> > .
> > For more options, visit this group at
> >http://groups.google.com/group/rubyonrails-talk?hl=en.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

No comments:

Post a Comment