I have three tables. users, posts, users_posts. This last one is to mark a post as read. How do I write to the users_post table? Here''s what I have: In model ''User_Post'' belongs_to :post belongs_to :user In post/view/show: <%= link_to "Mark As Read", :controller => ''post'', :action => ''post_read'', :user => @user.id :post => @post.id %> In post_controller: def post_read @postread = UserPost.new(params[:user :post]) if @postread.save flash[:notice] = "Read" end -- Posted via http://www.ruby-forum.com/.
//migration file: create_table :posts_users do |t| t.column :user_id, :integer t.column :post_id, :integer end Join tables, by default, are expected to have the tables they join in alpha order, hence posts_users rather than users_posts. comfort eagle wrote:> I have three tables. users, posts, users_posts. This last one is to > mark a post as read. > > How do I write to the users_post table? > > Here''s what I have: > > In model ''User_Post'' > belongs_to :post > belongs_to :user > > In post/view/show: > <%= link_to "Mark As Read", :controller => ''post'', :action => > ''post_read'', > :user => @user.id :post => @post.id %> > > In post_controller: > def post_read > @postread = UserPost.new(params[:user :post]) > if @postread.save > flash[:notice] = "Read" > end > >
David Chelimsky wrote:> //migration file: > create_table :posts_users do |t| > t.column :user_id, :integer > t.column :post_id, :integer > end > > Join tables, by default, are expected to have the tables they join in > alpha order, hence posts_users rather than users_posts.Thanks, then what? -- Posted via http://www.ruby-forum.com/.
comfort eagle wrote:> David Chelimsky wrote: > >> //migration file: >> ... >> Join tables ... > Thanks, then what?I thought you asked how to write the table, but now I see you asked how to write TO the table. Sorry for answering the wrong question :) DHH talked about this in his keynote at RailsConf - the idea is that you should think of PostUser (Posting?) as a first class citizen of your domain, and give it its own model and controller. In your case you may not need the controller, but if you give it a model you can do this in your PostsController (validation excluded for brevity): def create @post = Post.new(params[:post]) @post.save Posting.new(:user_id => params[:user_id], :post_id => @post.id).save end Cheers, David
David Chelimsky wrote:> comfort eagle wrote: >> David Chelimsky wrote: >> >>> //migration file: >>> ... >>> Join tables ... >> Thanks, then what? > I thought you asked how to write the table, but now I see you asked how > to write TO the table. Sorry for answering the wrong question :) > > DHH talked about this in his keynote at RailsConf - the idea is that you > should think of PostUser (Posting?) as a first class citizen of your > domain, and give it its own model and controller. In your case you may > not need the controller, but if you give it a model you can do this in > your PostsController (validation excluded for brevity): > > def create > @post = Post.new(params[:post]) > @post.save > Posting.new(:user_id => params[:user_id], :post_id => @post.id).save > end > > Cheers, > DavidInstead of "Posting" I have a model for table posts_users "PostUser". So substituting this: "PostUser.new(:user_id => params[:user_id], :post_id => @post.id).save" results in error: "blog_development.post_users'' doesn''t exist". Notice the singular "post" and multiple "users" now for the table name in the error msg. Ideas? -- Posted via http://www.ruby-forum.com/.
comfort eagle wrote:> Instead of "Posting" I have a model for table posts_users "PostUser". > > So substituting this: "PostUser.new(:user_id => params[:user_id], > :post_id => @post.id).save" results in error: > > "blog_development.post_users'' doesn''t exist". > > Notice the singular "post" and multiple "users" now for the table name > in the error msg. > > Ideas?Yup, rename to postings and Posting :-) The posts_users name is only relevant if you want the automagic of has_and_belongs_to_many. If you don''t, and you have a first class join model to link posts and users, I''d give it a better name. Alan -- Posted via http://www.ruby-forum.com/.
>> Instead of "Posting" I have a model for table posts_users "PostUser". >> >> So substituting this: "PostUser.new(:user_id => params[:user_id], >> :post_id => @post.id).save" results in error: >> >> "blog_development.post_users'' doesn''t exist". >> >> Notice the singular "post" and multiple "users" now for the table name >> in the error msg.rename to postings and Posting :-)> > The posts_users name is only relevant if you want the automagic of > has_and_belongs_to_many. If you don''t, and you have a first class join > model to link posts and users, I''d give it a better name.I do want the magic though. -- Posted via http://www.ruby-forum.com/.
You won''t get "the magic" because it''s not that kind of a join. If all you wanted was to map a user to a post, you could use "the magic" of has_and_belongs_to_many. Since you want to store additional information on that linking table, you need to make that linking table a new model. Then you use has_many :through to make it work the way you want. Here''s kinda what you want. class Readings< ActiveRecord::Base belongs_to :post belongs_to :user end class Post< ActiveRecord::Base has_many :readings, :dependent => :destroy has_many :users, :through => :readgins end class User< ActiveRecord::Base has_many :readings, :dependent => :destroy has_many :posts, :through => :readings end See this for some insight: http://blog.hasmanythrough.com/articles/2006/04/20/many-to-many-dance-off On 7/5/06, comfort eagle <steve@fooworks.com> wrote:> > >> Instead of "Posting" I have a model for table posts_users "PostUser". > >> > >> So substituting this: "PostUser.new(:user_id => params[:user_id], > >> :post_id => @post.id).save" results in error: > >> > >> "blog_development.post_users'' doesn''t exist". > >> > >> Notice the singular "post" and multiple "users" now for the table name > >> in the error msg. > > rename to postings and Posting :-) > > > > The posts_users name is only relevant if you want the automagic of > > has_and_belongs_to_many. If you don''t, and you have a first class join > > model to link posts and users, I''d give it a better name. > > I do want the magic though. > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060705/ef26fbbd/attachment-0001.html
"@postread = PostUser.new(:user_id => params[:user_id], :post_id => params[:post_id]).save" ended up working in my action. Maybe I''ll have more trouble getting that info out in a meaningful manner though. -- Posted via http://www.ruby-forum.com/.