Ruby on Rails Monday, September 6, 2010

On Sep 6, 10:26 am, Aruna Chinnamuthu <li...@ruby-forum.com> wrote:
> hi , i am new to ROR
>
> i was acually trying to write Ruby code for the api blogs
>
> i have written the ruby code as
>
>       Loading development environment (Rails 2.3.8)
>       >> class Blogpost < ActiveRecord::Base
>       >> has_many :taggings
>       >> has_many :tags,:through => :taggings
>       >> end
>       => nil
>      >> class Taggings < ActiveRecord::Base
>      >> belongs_to :blogpost
>     >> belongs_to :tag
>     >> end
>     => nil
>
>     ?> @tags=Tag.find_by_name("blog7")
>     => #<Tag id: 4, name: "blog7">
>     >> @taggings=Tagging.find_by_id(@tags.id)
>     => #<Tagging id: 4, tag_id: 1, taggable_id: 4, taggable_type:
> "Blogpost",    created_at: "2010-09-02 10:03:08">
>     >> @blogposts=Blogpost.find_by_id(@taggings.taggable_id)
>       => #<Blogpost id: 4, title: "blog post4", type: nil, slug:
> "blog-post4", description: "<p>BLOG desc 4</p>", meta: nil, user_id: 1,
> category_id: 379344121, times_viewed: 2, comments_count: 0, published:
> 1, created_at: "2010-09-02 10:03:08", updated_at: "2010-09-03 05:11:46",
> delta: false>
>
> Whether this way of writing Ruby code is the correct procedure for API..
>

If you had those associations then typically you'd use them, ie you'd
do stuff like @tag.taggings or @tag.blogposts rather than calling
find. It's also a little misleading the way you've used plurals
(@tags, @taggings etc...) for things that are only single objects, not
collections.

Fred
> Please give suggestions ..
> --
> 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@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