Souschef
2010-Jan-27 00:16 UTC
Newbie question: Where do I update the CRUD statements (if they exist)?
I''ve added a foreign key to a table (specifically, user_Id to my projects table so I can select ''myprojects'') and now when I add a project, the user_id is not being saved in the projects table. In a former life, I would update my SQL statements, but this is rails. Where do I update my CRUD statements? -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Marnen Laibow-Koser
2010-Jan-27 00:59 UTC
Re: Newbie question: Where do I update the CRUD statements (
Souschef wrote:> I''ve added a foreign key to a table (specifically, user_Id to my > projects table so I can select ''myprojects'') and now when I add a > project, the user_id is not being saved in the projects table.What errors are you getting?> In a > former life, I would update my SQL statements, but this is rails. > Where do I update my CRUD statements?Wherever they are. What ActiveRecord call is causing problems? Find it and fix it. Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Rails List
2010-Jan-27 05:03 UTC
Re: Newbie question: Where do I update the CRUD statements (if they exist)?
Souschef wrote:> I''ve added a foreign key to a table (specifically, user_Id to my > projects table so I can select ''myprojects'') and now when I add a > project, the user_id is not being saved in the projects table. In a > former life, I would update my SQL statements, but this is rails. > Where do I update my CRUD statements?download my craigslist clone software (developed in ror), go through them and learn. its real fun. http://github.com/railslist/craigslist-clone -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Adam Stegman
2010-Jan-27 12:58 UTC
Re: Newbie question: Where do I update the CRUD statements (if they exist)?
Can you show us the code that is not working? Make sure you have the relationship specified in your model so Rails knows it''s there. so you should have class Project < ActiveRecord::Base belongs_to :user end so that you can do: Project.new :user => @user or Project.new :user_id => 12 and then after project.save: project.user #=> #<User:...> project.user_id #=> 12 On Jan 26, 6:16 pm, Souschef <sgalva...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I''ve added a foreign key to a table (specifically, user_Id to my > projects table so I can select ''myprojects'') and now when I add a > project, the user_id is not being saved in the projects table. In a > former life, I would update my SQL statements, but this is rails. > Where do I update my CRUD statements?-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org 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.
pepe
2010-Jan-27 18:48 UTC
Re: Newbie question: Where do I update the CRUD statements (if they exist)?
I believe you will have to learn about associations (or relationships between tables, as another post has already noted). Look into ''belongs_to'' and ''has_*'' relationships (probably ''has_one'' or ''has_many''). That will most likely teach you what you need. On Jan 26, 7:16 pm, Souschef <sgalva...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I''ve added a foreign key to a table (specifically, user_Id to my > projects table so I can select ''myprojects'') and now when I add a > project, the user_id is not being saved in the projects table. In a > former life, I would update my SQL statements, but this is rails. > Where do I update my CRUD statements?-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org 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.
Souschef
2010-Jan-30 12:43 UTC
Re: Newbie question: Where do I update the CRUD statements (if they exist)?
Here are my models. I think the issue is how I have the user associated with the project. I recently integrated the authlogic plugin and associated the user to the project table (see below). I''m stumped because when I associated ''tasks'' to ''projects'', the foreign key for project was inserted into tasks. But for some reason now that I have ''users'' associated with projects, the user_id is not inserted into the project table on a save when a user is logged in. Should I be associating ''projects'' to the ''UserSession'' instead (model also included below, but currently empty)? Or do I need to declare some sort of logic that saves user_id when a user is actually logged in? Thanks in advance for input! --------------------Project model-------------------- class Project < ActiveRecord::Base validates_presence_of :name # allow ordering of tasks by step_number has_many :tasks, :dependent => :destroy, :order => ''step_number ASC'' accepts_nested_attributes_for :tasks, :reject_if => lambda { |a| a.values.all?(&:blank?) }, :allow_destroy => true def task_attributes=(task_attributes) task_attributes.each do |attributes| tasks.build(attributes) end end # Following statements tie Projects to users belongs_to :user end --------------------User model-------------------- class User < ActiveRecord::Base # following line commented out. Came from authlogic, but not sure # what it means… # attr_accessible :username, :email, :password # Added following line from railscast demo. Note: # http://github.com/binarylogic/authlogic_example # has an optional block for passing other config options, but didn’t # go there for now… acts_as_authentic has_many :projects end ------------------User Session model--------------- class UserSession < Authlogic::Session::Base end -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org 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.
Me
2010-Jan-30 15:40 UTC
Re: Newbie question: Where do I update the CRUD statements (if they exist)?
When you create a project you can do: @project = Project.find(params[:id]) @task = Task.create(params[:task]) @project.tasks << @task or something similar. this will automatically out the user id I the project table for you. On Jan 30, 6:43 am, Souschef <sgalva...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Here are my models. I think the issue is how I have the user > associated with the project. I recently integrated the authlogic > plugin and associated the user to the project table (see below). I''m > stumped because when I associated ''tasks'' to ''projects'', the foreign > key for project was inserted into tasks. But for some reason now that > I have ''users'' associated with projects, the user_id is not inserted > into the project table on a save when a user is logged in. Should I > be associating ''projects'' to the ''UserSession'' instead (model also > included below, but currently empty)? Or do I need to declare some > sort of logic that saves user_id when a user is actually logged in? > Thanks in advance for input! > > --------------------Project model-------------------- > class Project < ActiveRecord::Base > validates_presence_of :name > > # allow ordering of tasks by step_number > has_many :tasks, :dependent => :destroy, :order => ''step_number ASC'' > accepts_nested_attributes_for :tasks, :reject_if => lambda { |a| > a.values.all?(&:blank?) }, :allow_destroy => true > > def task_attributes=(task_attributes) > task_attributes.each do |attributes| > tasks.build(attributes) > end > end > > # Following statements tie Projects to users > belongs_to :user > > end > > --------------------User model-------------------- > class User < ActiveRecord::Base > # following line commented out. Came from authlogic, but not sure > # what it means… > # attr_accessible :username, :email, :password > > # Added following line from railscast demo. Note: > #http://github.com/binarylogic/authlogic_example > # has an optional block for passing other config options, but didn’t > # go there for now… > acts_as_authentic > > has_many :projects > end > > ------------------User Session model--------------- > class UserSession < Authlogic::Session::Base > end-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org 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.