Topic: Create a string that is owned by a user but related to another object

SO I want a user to be able to generate a code, like a 6 char alpha numeric code.  That should be pretty simple.  But I want that code to to relate to a band.  So the user has a code that relates to a band. I am newer to rails so am not sure on the best structure for this. 

I will be using rails 3.

thanks so much in advance

Re: Create a string that is owned by a user but related to another object

Is there a band model in your app?

Is there any relationship between band and user?

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.

Re: Create a string that is owned by a user but related to another object

There will be a band model.  Users in this case are fans of a band so there is not a relationship from a rails perspective.  The code will allow them to do different things related to the band later.

Re: Create a string that is owned by a user but related to another object

If bands have fans, and fans are your users,  then it follows that there might be a relationship.

Will you be tracking which users are fans of certain bands?

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.

Re: Create a string that is owned by a user but related to another object

I guess good point.  This is why I posted, because I needed help. 

So yes: Bands has_many :fans

The code is fan specific and band specific.  So a fan could be a fan of more than one band, and have a code for each.

So maybe the Create Code form would create the relationship.  The Code model would need a fan_id and a band_id right?

thanks

Re: Create a string that is owned by a user but related to another object

Classic example of the has_and_belongs_to_many association

http://api.rubyonrails.org/classes/Acti … gs_to_many

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.

Re: Create a string that is owned by a user but related to another object

So which piece is HABTM

Band has many fans
Fans has many bands

codes?

Re: Create a string that is owned by a user but related to another object

You have two choices,

has_many :through
http://guides.rubyonrails.org/associati … ssociation

or

has_and_belongs_to_many
http://guides.rubyonrails.org/associati … ssociation

to decide which you need
http://guides.rubyonrails.org/associati … gs_to_many

has_and_belongs_to_many requires a simple join table with ONLY two attributes,  band_id and user_id.

has_many :through allows you to create a join table with EXTRA attributes.  For example,  if you have an 'interest' tag.  Say a user selects an 'interest' tag of progressive rock,  then selects a band as one he considers 'progressive rock' AND he's a fan of,  and you want to record that tag along with the simple association,  you'd need the join table to have:

user_id
band_id
interest_tag

In that case you'd need has_many :through

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.

Re: Create a string that is owned by a user but related to another object

The primary difference between has_and_belongs_to_many (HABTM) and has_many :through (HMT) is that HMT introduces a new Model,  whereas HABTM does not.

With HABTM,  all you can do is

find all users who are fans of a band

or

find all bands who a user is a fan of.

With HMT,  you introduce a new model , let's call it 'Fanship'

As you introduce attributes into the Fanship model,  you can do things like

Find all bands who have fans because of 'interest_tag'

or

Find all Bands who have fans because of 'song_tag'

etc.

So you may end up with queries like

@song_fans = Fanship.find(:all, :conditions=>['band_id = ? AND song_id = ?', @band.id,@song.id])

or

@type_fans = Fanship.find(:all, :conditions=>['band_id = ? AND interest_tag = ?', @band.id, @interest.id])

And your Fanship model would be defined like

class AddFanship< ActiveRecord::Migration
    create_table :fanship do |t|
      t.integer  :band_id
      t.integer  :user_id
      t.integer  :interest_tag_id
      t.integer  :song_id
      t.timestamps
    end
end

Last edited by BradHodges (2011-10-31 00:43:41)

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.