Misham, you might want to check out the Simple Enum gem (
https://github.com/lwe/simple_enum)  which lets you easily add enumerated
types like your post_type to models and gives you lots of syntactic sugar to
boot.  If you need a column that can have multiple values, and dont want the
added complexity or performance penalty of a table join, the Bitmask
Attribute gem (https://github.com/joelmoss/bitmask_attributes)  is great for
that. Since it is just a bitmask stored as an integer in the DB, querying
for records with specific combinations of values is very fast.

Regards,

John Lynch
[email protected]



On Tue, Oct 18, 2011 at 5:49 PM, misham <[email protected]> wrote:

> Adam,
>
> Thanks for the suggestions.
>
> I ended up not using another table, but just adding a post_type
> column to the Post model.  I think it works much simpler this way.
>
> I ended up defining the values as a constant hash inside the Post
> model
> and creating a validation that checks submitted value is one of the
> values
> in the hash constant.
>
> Thanks for the links, as well!
>
> Misha
>
> On Oct 18, 1:15 pm, Adam Grant <[email protected]> wrote:
> > Hi Misha,
> >
> > belongs_to/has_many is the ideal here.
> >
> >  it "should have the right post type"
> >     post_type = PostType.first
> >     post = Post.create(@attr.merge(:post_type => post_type))
> >     post.post_type.should == post_type # or  post.post_type_id.should ==
> > post_type.id
> >     post.errors.should be_empty?   # not sure about the Rspec syntax
> here,
> > something like that though
> >  end
> >
> > If you want to add validations, then you can also write a spec to create
> the
> > post without the post_type, try saving, and add an assertion that an
> error
> > about empty post_type shows up.
> >
> > I would look into FactoryGirl for database records instead of fixtures.
> We
> > use it here at Sony, and it works amazingly.
> >
> > Personally, I wouldn't test the associations on the model too much. They
> are
> > pretty well tested on the Rails end. I would test the controller actions
> > that assign params to the object though and save it (in a functional test
> > for the controller), because that is code you will have to write, to make
> > sure it goes the full stack. That would be a more useful test. But if you
> > are just trying to learn, then this is a good exercise, what you are
> doing
> > here.
> >
> > Resources:
> http://guides.rubyonrails.org/http://railsforzombies.org/http://www.codeschool.com/courses/rails-best-practices
> > railscasts.com
> >
> > Hope that helps,
> > Adam
> >
> >
> >
> >
> >
> >
> >
> > On Mon, Oct 17, 2011 at 11:06 PM, misham <[email protected]> wrote:
> > > Hi,
> >
> > > I'm trying to figure out how to test a relationship between two models
> > > using RSpec and Rails 3.1
> >
> > > I included a RSpec file with a sample test that passes.  I'm trying to
> > > write a test that will validate assignment of one of the Post Types
> > > and check that it was assigned correctly.  This testing the same
> > > behavior as picking a Post Type using a drop-down menu and having it
> > > saved with the new Post.
> >
> > > * How would I write the last test in the sample RSpec file below?
> > > * Is there a good tutorial or resource on testing this kind of
> > > behavior?
> > > * Is this a good way to represent this association? Would has_one/
> > > has_many be better instead of belongs_to/has_many?
> >
> > > Thank you
> >
> > > - Misha
> >
> > > -------------
> > > Code:
> >
> > > The sample data in the Post Types is:
> >
> > > id | value | ...
> > > 1  | foo    | ...
> > > 2  | bar    | ...
> > > 3  | baz   | ...
> >
> > > # app/models/post_type.rb
> > > class PostType < ActiveRecord::Base
> > >    attr_reader :name
> >
> > >    has_many :posts
> > > end
> >
> > > # app/models/post.rb
> > > class Post < ActibeRecord::Base
> > >    attr_accessible :content
> >
> > >    belongs_to :post_type
> > > end
> >
> > > # spec/models/post_spec.rb
> > > describe Post do
> > >  before :each do
> > >    @attr = { :content = "Lorem ipsum" }
> > >  end
> >
> > >  # ... other tests
> >
> > >  describe "post type association" do
> > >    before :each do
> > >      @post = Post.create @attr
> > >    end
> >
> > >    it "should have a post type attribute" do
> > >      @post.should respond_to :post_type
> > >    end
> >
> > >    it "should have the right post type"
> > >  end
> > > end
> >
> > > --
> > > SD Ruby mailing list
> > > [email protected]
> > >http://groups.google.com/group/sdruby
>
> --
> SD Ruby mailing list
> [email protected]
> http://groups.google.com/group/sdruby
>

-- 
SD Ruby mailing list
[email protected]
http://groups.google.com/group/sdruby

Reply via email to