Hello, i want to know if i am relationing fine, because i am having problems,i have 2 models, one for members and other to relate some members to this: class Member < ActiveRecord::Base has_many :matrix, :foreign_key=>:parent end class Matrix < ActiveRecord::Base set_table_name :matrix set_primary_key :userid belongs_to :member, :foreign_key=>:userid acts_as_tree :foreign_key=>:parent end when i try to save a new member it says: undefined method `userid='' for Matrix:Class and i think is because he wants to do find() first, but i dont want it, because i am saving a new. def create @member = Member.new(params[:member]) @member.matrix.new @member.matrix.userid = @member.userid @member.matrix.parent = params[:parent] .... end is a relationating problem?? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
rein.henrichs-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Sep-03 06:32 UTC
Re: relationing has_many, belongs_to
userid cannot be both the primary key and a foreign key. Rein On Sep 2, 11:48 pm, Edgar Gonzalez <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hello, i want to know if i am relationing fine, because i am having > problems,i have 2 models, one for members and other to relate some > members to this: > > class Member < ActiveRecord::Base > has_many :matrix, :foreign_key=>:parent > end > > class Matrix < ActiveRecord::Base > set_table_name :matrix > set_primary_key :userid > belongs_to :member, :foreign_key=>:userid > acts_as_tree :foreign_key=>:parent > end > > when i try to save a new member it says: undefined method `userid='' for > Matrix:Class and i think is because he wants to do find() first, but i > dont want it, because i am saving a new. > > def create > @member = Member.new(params[:member]) > @member.matrix.new > @member.matrix.userid = @member.userid > @member.matrix.parent = params[:parent] > .... > end > > is a relationating problem?? > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Well the problem is, that the table schema is thus, because in some case i would rather use the rails convention an use member_id, in case of userid, but i have to work with the actual table. but my problem i dont think that is with this. by the way, thanks Rein -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
@member.matrix.new returns a new matrix object. you have to assign it to a variable: def create @member = Member.new(params[:member]) @new_matrix = @member.matrix.new @new_matrix.userid = @member.userid @new_matrix.parent = params[:parent] .... end about foreign keys: in the has_many, you define parent as the foreign key, in the belongs_to, you define userid as foreign key. but it has to be the same on both sides, otherwise it''s no proper relation, legacy Database or not. You tell the Member model that is has many Matrix'' associated through the parent column in the matrix table, but you tell the Matric Model that it belongs to a Member associated by the userid column in the matrix table ... it should say "parent" here to, because the table on the has_many side doesn''t have a foreign key, only the belongs_ to side On 3 Sep., 13:01, Edgar Gonzalez <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Well the problem is, that the table schema is thus, because in some case > i would rather use the rails convention an use member_id, in case of > userid, but i have to work with the actual table. > > but my problem i dont think that is with this. > > by the way, thanks Rein > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks Thorsten, I got first part, but about the correct relation, i didn''t understood right, could you explain me better? Thanks -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
i still got the problem when i try to save through has_many def create @member = Member.new(params[:member]) @new_matrix = Matrix.new @new_matrix.parent = params[:parent] respond_to do |format| if @member.save @new_matrix.userid = @member.id @new_matrix.save ..... end end end undefined method `userid='' for #<Matrix:0x48a4450> but there is a column called userid! :S i am consufused now -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
You''ve defined userid as the primary key, which makes it accessable as id (ie @matrix.id) rather than userid. The problem here, though, is manifold: Rails expects your database to be sane, which includes an id column that is a unique auto-incrementing integer. Using it as a foreign key means that you''ll have to *set* the primary key of the record, which is a huge no-no in both rails and database design and violates the auto-incrementing requirement. Using is as a foreign key also implies that two records could share the same foreign key, and therefore the same primary key, which violates the unique requirement. Rails can work with most legacy databases provided that they''re relatively sane. Using a primary key as a foreign key is most definitely insane and I doubt you''d make that table work properly with or without rails. Rein On Sep 3, 1:31 pm, Edgar Gonzalez <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> i still got the problem when i try to save through has_many > > def create > @member = Member.new(params[:member]) > @new_matrix = Matrix.new > @new_matrix.parent = params[:parent] > > respond_to do |format| > if @member.save > @new_matrix.userid = @member.id > @new_matrix.save > ..... > end > end > end > > undefined method `userid='' for #<Matrix:0x48a4450> > > but there is a column called userid! :S > > i am consufused now > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks a lot Rein, the problem is that it was a old system and now i am trying to move on rails. now i got: class Matrix < ActiveRecord::Base set_table_name :matrix belongs_to :member, :foreign_key=>:userid acts_as_tree :foreign_key=>:parent attr_writer :name attr_writer :city attr_writer :address attr_writer :identification attr_writer :phone attr_writer :sponsor attr_writer :email end i have put all these ''att_r_writer'' because this: def create @member = Member.new(params[:member]) @new_matrix = Matrix.new @new_matrix.parent = params[:member] <<<<--- here is my problem respond_to do |format| if @member.save @new_matrix.userid = @member.id @new_matrix.save ...... end end how can i access to the params[:parent]??, because tis param is inside the params[:member] Request Parameters: {"commit"=>"Create", "member"=>{"name"=>"Edgar /8", "city"=>"sdfsdf", "identification"=>"s44546", "phone"=>"N/A", "sponsor"=>"0", "address"=>"sd", "email"=>"sdf-KdrC/hUTNrA@public.gmane.org", "parent"=>"5"}} i tried params[:member][:parent], params[:parent] -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---