Welcome Guest | Login

one form with two models

Hi,

My problem is "simple" yet complicated.

I have a 1:1 relation between user and profile.
User Model: has_one :profile
Profile Model: belongs_to :user

then i have a form to create a new user
<% form_tag('/users/create', :method => :post, :multipart => true) do %>
(...)
ok, the fields and submit button work as i get the table user populated with the data from the form

but, it should also create a row in the profile table with the correspondent user_id wich is not
heres my controller code:
   def new
    @user = User.new
    @profile = Profile.new    
  end
  def create
    cookies.delete :auth_token
    @user = User.new(params[:user])
    @profile = @user.build_profile(params[:profile])    
    @user.save!
    render :action => 'verify'
   rescue ActiveRecord::RecordInvalid
    render :action => 'new'
 end
Where am i missing?

Is a 1:1 relation that complicated?
i know that with has_many works fine as i tried before

when i create a user it should have a row in user table with the data i inserted (working) and a row in profile table with the user_id and the rest null.

any suggestions? :D

2008-05-30 05:45 PM

code is poetry~
Hi Marcobr,

>>ok, the fields and submit button work as i get the table user >populated with the data from the form but, it should also >create a row in the profile table with the correspondent >user_id wich is not heres my controller code:

The 1:1 relationship is maintained between two resources by a default row in the "belongs_to" class (Profile) which represents to the "has_one" class (User). So there will be a foreign key of has_one class "User" present inside the belongs_to class "Profile".

The following wiki deals with the 1:1 relationship in detail please have a look at ithttp://wiki.rubyonrails.org/rails/pages/has_one

>>when i create a user it should have a row in user table with the data i inserted (working) and a row in profile table with the user_id and the rest null.

Have you specified the relationship in the environment .rb as shown below:

==
map.resources :user, :has_one => :profile
==

The value will be present inside the profile table if you entered the values to the profile table using the User_id:, you can test that from the backed (script/console of your rails app) by following the following steps.

eg:-
===
user1=User.create(:row1=>'value',:row2=>'test'...)
profile1=Profile.create :profile_row => 'value'
user1.profiles<<profile1
==

Please check whether the profile table is updated according to the User_id.

2008-05-30 08:25 PM

map.resources :user, :has_one => :profile

you sure that the map.resources is necessary?

i inserted that in enviroment.rb and i got:
"./config/environment.rb:13: undefined local variable or method `map' for main:Object (NameError)"


I'm not so good with script/console, but from what you said it should be :
@profile=Profile.create(params[:profile])
@user.profile<<@profile
you think i should try that way?

i just don't get how could this be so complicated
it was said that if you have a has_one you could build with build_child:
@profile = user.build_profile(params[:profile]) 
that should work, but it doesn't

i'm thinking about merging the table user with profile, and avoid using the has_one relationship as it's not thaat usefull as has_many or others

2008-05-31 02:31 AM

code is poetry~
I'd suggest indeed merging the table user with profile - that has_one relationship is commonly troubling.  

2008-05-31 07:27 PM

thanks for the opinion William.

I'm holding myself not to because it's something so simple and i see it in some codes working properly except mine


in my situation, would you merge the tables eventhough they would have around 35 columns?

doesn't that sound wrong somehow?

i know that, mysql speaking, 1:1 is almost never necessary and in my case can even increase performance as it's doing a single table consult, right?

but i need more opinion about this, i don't want to regret it in the future

2008-06-01 05:07 AM

code is poetry~
Yeah I understand.  35 columns isn't too bad - but like you I see no reason why that build_profile would fail.    Is it working locally for you and not online?  

2008-06-01 11:16 AM

i didn't test locally (windows here)

the funny thing is that it was working before but not anymore

i already tried coming back on the SVN or undoing the code, but nothing
so i really think the has_one is troubling

i believe i will have to merge

is it possible to still have the profile model but getting just the contact information from the user table?

2008-06-01 11:46 PM

code is poetry~
Sure you can have the profile model - if anything you can just use the has_many and always just use the first record in the array.  If you'd like to paste exactly what you have in the user and profile models (for the has_ belongs_to parts) we can take a closer look.  

2008-06-02 12:44 AM

William, i already merged.

Couldn't resist seeing everything working smoothly like it should.

Sometimes i think rails is "too easy" that it can become complicated in the end, at least i feel that way.

Also, to make the code more elegant i was reading about "Aggregation" with the use of composed_of in the model.
I think that will do :D

Thank you for all the opinions and help.

2008-06-03 12:49 PM

code is poetry~
well, i have another problem lol

this post could be in the other topic about 'validates_acceptance_of', but here seems to be the correct place for it

after merging both tables, profile+user, i have one single gigant user table

this is the proccess:
- the user signup, a form (view) is presented with a few required information such as login,password,email AND the terms that he has to accept, the other optional information area all saved as nill
- after signing up, validating the email, he can login
- the user now login and edit his profile, adding that optional information (what should be the profile table) such as website, msn, city, country, etc..

and now comes the problem: when he edits the information and click 'submit' an error raises:
"You must read and accept the terms of privacy"
and there is no terms in that view, its because of the validation in the user model..
(funny tought that this is the only validation that is complaining)

the first idea i got was using hidden fields:
<input type="hidden" id="terms" name="terms" value="yes" />
<input type="hidden" id="privacy" name="privacy" value="yes" />
but did not work..

should i regret that i had merged the both tables?

2008-06-19 12:43 PM

code is poetry~
okay, this is the best solution that i've got, looks bad  but i like to see the app working properly:

<% check_box_tag 'terms', 'yes', true %>
without the "=", doesn't show the check_box but it's still there for the rails

i'm open to suggestions

thank you anyway.

2008-06-19 01:51 PM

code is poetry~

Hello Guest! In order to post you must be an active client with us, please log in or sign up today!