I''m relatively new to rails and am setting up a system which contains bands and users. Both bands and users has and belongs to many of the other. In the bands "show" view, I want to list all the users within a band. I do this by using the code: <% for user in @band.users %> and iterating through the results. However, I want to display a special div if the current user''s id (stored in the session variabel "@current_user.id") is within the returned results. I have unsuccessfully tried doing the following: <% for user in @band.users %> Member name:<%=h user.login %> <br> <% if user.user_id == @current_user.id %> *DISPLAY SPECIAL DIV* <% end %> <% end %> I can''t understand why this isn''t working. Can anyone advise me on this? --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Jul 17, 3:10 pm, Gearóid O''Ceallaigh <gearoi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> <% if user.user_id == @current_user.id %> > *DISPLAY SPECIAL DIV* > <% end %> > > <% end %> > > I can''t understand why this isn''t working. Can anyone advise me on > this?I think you mean this instead? if user.id == @current_user.id (instead of user.user_id == .....) Jeff softiesonrails.com purpleworkshops.om --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@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 -~----------~----~----~----~------~----~------~--~---
the line <% if user.user_id == @current_user.id %> should be <% if user.id == @current_user.id %> On Jul 17, 10:10 pm, Gearóid O''Ceallaigh <gearoi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I''m relatively new to rails and am setting up a system which contains > bands and users. Both bands and users has and belongs to many of the > other. > > In the bands "show" view, I want to list all the users within a band. > I do this by using the code: > > <% for user in @band.users %> > > and iterating through the results. > > However, I want to display a special div if the current user''s id > (stored in the session variabel "@current_user.id") is within the > returned results. I have unsuccessfully tried doing the following: > > <% for user in @band.users %> > > Member name:<%=h user.login %> <br> > > <% if user.user_id == @current_user.id %> > *DISPLAY SPECIAL DIV* > <% end %> > > <% end %> > > I can''t understand why this isn''t working. Can anyone advise me on > this?--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@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 -~----------~----~----~----~------~----~------~--~---
Maybe you''re storing the user_id within session[:user_id]? session[:user_id] == user.id On Jul 17, 1:45 pm, Dejan Dimic <dejan.di...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> the line > > <% if user.user_id == @current_user.id %> > > should be > > <% if user.id == @current_user.id %> > > On Jul 17, 10:10 pm, Gearóid O''Ceallaigh <gearoi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > I''m relatively new to rails and am setting up a system which contains > > bands and users. Both bands and users has and belongs to many of the > > other. > > > In the bands "show" view, I want to list all the users within a band. > > I do this by using the code: > > > <% for user in @band.users %> > > > and iterating through the results. > > > However, I want to display a special div if the current user''s id > > (stored in the session variabel "@current_user.id") is within the > > returned results. I have unsuccessfully tried doing the following: > > > <% for user in @band.users %> > > > Member name:<%=h user.login %> <br> > > > <% if user.user_id == @current_user.id %> > > *DISPLAY SPECIAL DIV* > > <% end %> > > > <% end %> > > > I can''t understand why this isn''t working. Can anyone advise me on > > this?--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@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 -~----------~----~----~----~------~----~------~--~---
I think my relationships may be set up incorrectly but I''m not sure why. I guess I''d be best to give a bit of background. I''m setting up a musicians network for my local area. There are bands and users as described above, and these are linked with the table "bands_users" which has three fields: id, band_id, and user_id. I am also working off the Beast forum so I thought it would be as easy to use their user table and configuration since its already set up. So, what I find strange is that, when I want to return the user id like above, I must use "user.user_id". I believe I should be able to simply state user.id but doing so returns the value from "id" (note: NOT user_id - just the unique id from the bands_users table). I also have a similar relationship set up between bands and gigs, but this works as desired. Does anyone have any ideas why this would be messing up? Would the fact that I''m taking the data from the Beast Forum''s users be causing problems. Any help, as ever, is appreciated. On Jul 17, 9:45 pm, Dejan Dimic <dejan.di...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> the line > > <% if user.user_id == @current_user.id %> > > should be > > <% if user.id == @current_user.id %> > > On Jul 17, 10:10 pm, Gearóid O''Ceallaigh <gearoi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > I''m relatively new to rails and am setting up a system which contains > > bands and users. Both bands and users has and belongs to many of the > > other. > > > In the bands "show" view, I want to list all the users within a band. > > I do this by using the code: > > > <% for user in @band.users %> > > > and iterating through the results. > > > However, I want to display a special div if the current user''s id > > (stored in the session variabel "@current_user.id") is within the > > returned results. I have unsuccessfully tried doing the following: > > > <% for user in @band.users %> > > > Member name:<%=h user.login %> <br> > > > <% if user.user_id == @current_user.id %> > > *DISPLAY SPECIAL DIV* > > <% end %> > > > <% end %> > > > I can''t understand why this isn''t working. Can anyone advise me on > > this?--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@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 -~----------~----~----~----~------~----~------~--~---
On 18 Jul 2008, at 23:42, Gearóid O''Ceallaigh wrote:> > I think my relationships may be set up incorrectly but I''m not sure > why. I guess I''d be best to give a bit of background. >If you thing that''s the case, then maybe showing us how you''re setting them up would enlighten everyone. Fred> I''m setting up a musicians network for my local area. There are bands > and users as described above, and these are linked with the table > "bands_users" which has three fields: id, band_id, and user_id. I am > also working off the Beast forum so I thought it would be as easy to > use their user table and configuration since its already set up. > > So, what I find strange is that, when I want to return the user id > like above, I must use "user.user_id". I believe I should be able to > simply state user.id but doing so returns the value from "id" (note: > NOT user_id - just the unique id from the bands_users table). > > I also have a similar relationship set up between bands and gigs, but > this works as desired. > > Does anyone have any ideas why this would be messing up? Would the > fact that I''m taking the data from the Beast Forum''s users be causing > problems. Any help, as ever, is appreciated. > > On Jul 17, 9:45 pm, Dejan Dimic <dejan.di...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> the line >> >> <% if user.user_id == @current_user.id %> >> >> should be >> >> <% if user.id == @current_user.id %> >> >> On Jul 17, 10:10 pm, Gearóid O''Ceallaigh <gearoi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> >>> I''m relatively new to rails and am setting up a system which >>> contains >>> bands and users. Both bands and users has and belongs to many of the >>> other. >> >>> In the bands "show" view, I want to list all the users within a >>> band. >>> I do this by using the code: >> >>> <% for user in @band.users %> >> >>> and iterating through the results. >> >>> However, I want to display a special div if the current user''s id >>> (stored in the session variabel "@current_user.id") is within the >>> returned results. I have unsuccessfully tried doing the following: >> >>> <% for user in @band.users %> >> >>> Member name:<%=h user.login %> <br> >> >>> <% if user.user_id == @current_user.id %> >>> *DISPLAY SPECIAL DIV* >>> <% end %> >> >>> <% end %> >> >>> I can''t understand why this isn''t working. Can anyone advise me on >>> this? > >--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@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 -~----------~----~----~----~------~----~------~--~---
If its enlightenment you seek, then I''d suggest reading the entire post. Regardless, here is the code if you couldn''t figure it out: band.rb *********************************************************** class Band < ActiveRecord::Base has_and_belongs_to_many :gigs has_and_belongs_to_many :users def self.get_bands find (:all, :order => ''band_name'') end end user.rb ********************************************** class User < ActiveRecord::Base has_and_belongs_to_many :bands def self.per_page 50 end has_many :moderatorships, :dependent => :destroy has_many :forums, :through => :moderatorships, :order => "#{Forum.table_name}.name" has_many :posts has_many :topics has_many :monitorships has_many :monitored_topics, :through => :monitorships, :conditions => ["#{Monitorship.table_name}.active = ?", true], :order => "#{Topic.table_name}.replied_at desc", :source => :topic validates_presence_of :login, :email validates_length_of :login, :minimum => 2 with_options :if => :password_required? do |u| u.validates_presence_of :password_hash u.validates_length_of :password, :minimum => 5, :allow_nil => true u.validates_confirmation_of :password, :on => :create u.validates_confirmation_of :password, :on => :update, :allow_nil => true end # names that start with #s really upset me for some reason validates_format_of :login, :with => /^[a-z]{2}(?:\w+)?$/i # names that start with #s really upset me for some reason validates_format_of :display_name, :with => /^[a-z]{2}(?:[.''\- \w ]+)?$/i, :allow_nil => true validates_format_of :email, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+ [a-z]{2,})$/i, :message => "Please check the e-mail address"[:check_email_message] validates_uniqueness_of :email validates_uniqueness_of :login, :case_sensitive => false validates_uniqueness_of :display_name, :openid_url, :case_sensitive => false, :allow_nil => true before_validation { |u| u.display_name = u.login if u.display_name.blank? } # first user becomes admin automatically before_create { |u| u.admin = u.activated = true if User.count =0 } before_save { |u| u.email.downcase! if u.email } format_attribute :bio attr_reader :password attr_protected :admin, :posts_count, :login, :created_at, :updated_at, :last_login_at, :topics_count, :activated def self.currently_online User.find(:all, :conditions => ["last_seen_at > ?", Time.now.utc-5.minutes]) end # we allow false to be passed in so a failed login can check # for an inactive account to show a different error def self.authenticate(login, password, activated=true) find_by_login_and_password_hash_and_activated(login, Digest::SHA1.hexdigest(password + PASSWORD_SALT), activated) end def self.search(query, options = {}) with_scope :find => { :conditions => build_search_conditions(query) } do options[:page] ||= nil paginate options end end def self.build_search_conditions(query) query && [''LOWER(display_name) LIKE :q OR LOWER(login) LIKE :q'', {:q => "%#{query}%"}] end def password=(value) return if value.blank? write_attribute :password_hash, Digest::SHA1.hexdigest(value + PASSWORD_SALT) @password = value end def openid_url=(value) write_attribute :openid_url, value.blank? ? nil : OpenIdAuthentication.normalize_url(value) end def reset_login_key(rehash = true) # this is not currently honored self.login_key_expires_at = Time.now.utc+1.year self.login_key = Digest::SHA1.hexdigest(Time.now.to_s + password_hash.to_s + rand(123456789).to_s).to_s if rehash end def reset_login_key!(rehash = true) reset_login_key(rehash) save! login_key end def active_login_key reset_login_key!(login_key.blank?) end def moderator_of?(forum) moderatorships.count("#{Moderatorship.table_name}.id", :conditions => [''forum_id = ?'', (forum.is_a?(Forum) ? forum.id : forum)]) == 1 end def to_xml(options = {}) options[:except] ||= [] options[:except] << :email << :login_key << :login_key_expires_at << :password_hash << :openid_url << :activated << :admin super end def password_required? openid_url.nil? end def update_posts_count self.class.update_posts_count id end def self.update_posts_count(id) User.update_all [''posts_count = ?'', Post.count(:id, :conditions => {:user_id => id})], [''id = ?'', id] end end On Jul 19, 12:17 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 18 Jul 2008, at 23:42, Gearóid O''Ceallaigh wrote: > > > > > I think my relationships may be set up incorrectly but I''m not sure > > why. I guess I''d be best to give a bit of background. > > If you thing that''s the case, then maybe showing us how you''re setting > them up would enlighten everyone. > > Fred > > > I''m setting up a musicians network for my local area. There are bands > > and users as described above, and these are linked with the table > > "bands_users" which has three fields: id, band_id, and user_id. I am > > also working off the Beast forum so I thought it would be as easy to > > use their user table and configuration since its already set up. > > > So, what I find strange is that, when I want to return the user id > > like above, I must use "user.user_id". I believe I should be able to > > simply state user.id but doing so returns the value from "id" (note: > > NOT user_id - just the unique id from the bands_users table). > > > I also have a similar relationship set up between bands and gigs, but > > this works as desired. > > > Does anyone have any ideas why this would be messing up? Would the > > fact that I''m taking the data from the Beast Forum''s users be causing > > problems. Any help, as ever, is appreciated. > > > On Jul 17, 9:45 pm, Dejan Dimic <dejan.di...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >> the line > > >> <% if user.user_id == @current_user.id %> > > >> should be > > >> <% if user.id == @current_user.id %> > > >> On Jul 17, 10:10 pm, Gearóid O''Ceallaigh <gearoi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > >>> I''m relatively new to rails and am setting up a system which > >>> contains > >>> bands and users. Both bands and users has and belongs to many of the > >>> other. > > >>> In the bands "show" view, I want to list all the users within a > >>> band. > >>> I do this by using the code: > > >>> <% for user in @band.users %> > > >>> and iterating through the results. > > >>> However, I want to display a special div if the current user''s id > >>> (stored in the session variabel "@current_user.id") is within the > >>> returned results. I have unsuccessfully tried doing the following: > > >>> <% for user in @band.users %> > > >>> Member name:<%=h user.login %> <br> > > >>> <% if user.user_id == @current_user.id %> > >>> *DISPLAY SPECIAL DIV* > >>> <% end %> > > >>> <% end %> > > >>> I can''t understand why this isn''t working. Can anyone advise me on > >>> this?--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@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 -~----------~----~----~----~------~----~------~--~---
On 20 Jul 2008, at 19:04, Gearóid O''Ceallaigh wrote:> > If its enlightenment you seek, then I''d suggest reading the entire > post. Regardless, here is the code if you couldn''t figure it out: >Shot in the dark: does your join table between bands and users have an id column ? It shouldn''t. If rails is just doing a select * then the id from the join table could easily clobber the id from the users table. Fred> band.rb > *********************************************************** > class Band < ActiveRecord::Base > has_and_belongs_to_many :gigs > has_and_belongs_to_many :users > > def self.get_bands > find (:all, :order => ''band_name'') > end > > > end > > > user.rb > ********************************************** > > class User < ActiveRecord::Base > has_and_belongs_to_many :bands > > def self.per_page > 50 > end > > has_many :moderatorships, :dependent => :destroy > has_many :forums, :through => :moderatorships, :order => > "#{Forum.table_name}.name" > > has_many :posts > has_many :topics > has_many :monitorships > has_many :monitored_topics, :through => :monitorships, :conditions > => ["#{Monitorship.table_name}.active = ?", true], :order => > "#{Topic.table_name}.replied_at desc", :source => :topic > > validates_presence_of :login, :email > validates_length_of :login, :minimum => 2 > > with_options :if => :password_required? do |u| > u.validates_presence_of :password_hash > u.validates_length_of :password, :minimum => 5, :allow_nil > => true > u.validates_confirmation_of :password, :on => :create > u.validates_confirmation_of :password, :on => :update, :allow_nil > => true > end > > # names that start with #s really upset me for some reason > validates_format_of :login, :with => /^[a-z]{2}(?:\w+)?$/i > > # names that start with #s really upset me for some reason > validates_format_of :display_name, :with => /^[a-z]{2}(?:[.''\- > \w ]+)?$/i, :allow_nil => true > > validates_format_of :email, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+ > [a-z]{2,})$/i, :message => "Please check the e-mail > address"[:check_email_message] > > validates_uniqueness_of :email > validates_uniqueness_of :login, :case_sensitive => false > validates_uniqueness_of :display_name, :openid_url, :case_sensitive > => false, :allow_nil => true > before_validation { |u| u.display_name = u.login if > u.display_name.blank? } > # first user becomes admin automatically > before_create { |u| u.admin = u.activated = true if User.count => 0 } > before_save { |u| u.email.downcase! if u.email } > format_attribute :bio > > attr_reader :password > > attr_protected > :admin > , :posts_count > , :login > , :created_at, :updated_at, :last_login_at, :topics_count, :activated > > def self.currently_online > User.find(:all, :conditions => ["last_seen_at > ?", > Time.now.utc-5.minutes]) > end > > # we allow false to be passed in so a failed login can check > # for an inactive account to show a different error > def self.authenticate(login, password, activated=true) > find_by_login_and_password_hash_and_activated(login, > Digest::SHA1.hexdigest(password + PASSWORD_SALT), activated) > end > > def self.search(query, options = {}) > with_scope :find => { :conditions => > build_search_conditions(query) } do > options[:page] ||= nil > paginate options > end > end > > def self.build_search_conditions(query) > query && [''LOWER(display_name) LIKE :q OR LOWER(login) LIKE :q'', > {:q => "%#{query}%"}] > end > > def password=(value) > return if value.blank? > write_attribute :password_hash, Digest::SHA1.hexdigest(value + > PASSWORD_SALT) > @password = value > end > > def openid_url=(value) > write_attribute :openid_url, value.blank? ? nil : > OpenIdAuthentication.normalize_url(value) > end > > def reset_login_key(rehash = true) > # this is not currently honored > self.login_key_expires_at = Time.now.utc+1.year > self.login_key = Digest::SHA1.hexdigest(Time.now.to_s + > password_hash.to_s + rand(123456789).to_s).to_s if rehash > end > > def reset_login_key!(rehash = true) > reset_login_key(rehash) > save! > login_key > end > > def active_login_key > reset_login_key!(login_key.blank?) > end > > def moderator_of?(forum) > moderatorships.count("#{Moderatorship.table_name}.id", :conditions > => [''forum_id = ?'', (forum.is_a?(Forum) ? forum.id : forum)]) == 1 > end > > def to_xml(options = {}) > options[:except] ||= [] > options[:except] << :email << :login_key << :login_key_expires_at > << :password_hash << :openid_url << :activated << :admin > super > end > > def password_required? > openid_url.nil? > end > > def update_posts_count > self.class.update_posts_count id > end > > def self.update_posts_count(id) > User.update_all [''posts_count = ?'', Post.count(:id, :conditions => > {:user_id => id})], [''id = ?'', id] > end > end > > > On Jul 19, 12:17 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: >> On 18 Jul 2008, at 23:42, Gearóid O''Ceallaigh wrote: >> >> >> >>> I think my relationships may be set up incorrectly but I''m not sure >>> why. I guess I''d be best to give a bit of background. >> >> If you thing that''s the case, then maybe showing us how you''re >> setting >> them up would enlighten everyone. >> >> Fred >> >>> I''m setting up a musicians network for my local area. There are >>> bands >>> and users as described above, and these are linked with the table >>> "bands_users" which has three fields: id, band_id, and user_id. I am >>> also working off the Beast forum so I thought it would be as easy to >>> use their user table and configuration since its already set up. >> >>> So, what I find strange is that, when I want to return the user id >>> like above, I must use "user.user_id". I believe I should be able to >>> simply state user.id but doing so returns the value from "id" (note: >>> NOT user_id - just the unique id from the bands_users table). >> >>> I also have a similar relationship set up between bands and gigs, >>> but >>> this works as desired. >> >>> Does anyone have any ideas why this would be messing up? Would the >>> fact that I''m taking the data from the Beast Forum''s users be >>> causing >>> problems. Any help, as ever, is appreciated. >> >>> On Jul 17, 9:45 pm, Dejan Dimic <dejan.di...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >>>> the line >> >>>> <% if user.user_id == @current_user.id %> >> >>>> should be >> >>>> <% if user.id == @current_user.id %> >> >>>> On Jul 17, 10:10 pm, Gearóid O''Ceallaigh <gearoi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> >>>> wrote: >> >>>>> I''m relatively new to rails and am setting up a system which >>>>> contains >>>>> bands and users. Both bands and users has and belongs to many of >>>>> the >>>>> other. >> >>>>> In the bands "show" view, I want to list all the users within a >>>>> band. >>>>> I do this by using the code: >> >>>>> <% for user in @band.users %> >> >>>>> and iterating through the results. >> >>>>> However, I want to display a special div if the current user''s id >>>>> (stored in the session variabel "@current_user.id") is within the >>>>> returned results. I have unsuccessfully tried doing the following: >> >>>>> <% for user in @band.users %> >> >>>>> Member name:<%=h user.login %> <br> >> >>>>> <% if user.user_id == @current_user.id %> >>>>> *DISPLAY SPECIAL DIV* >>>>> <% end %> >> >>>>> <% end %> >> >>>>> I can''t understand why this isn''t working. Can anyone advise me on >>>>> this? > >--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@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 -~----------~----~----~----~------~----~------~--~---
Its begun to work now (added an else statement) but thanks for the advice anyway. What I''d like to is to perform a check to see if a user is in a band within a controller. I presume I should put something along the following in the bands controller: def is_member_in_band return if admin? for user in @band.users if user.id == @current_user.id return true end end end Is there way of simply checking if the collection of users in @band.users contains the current user''s id without having to loop through them? Thanks to everyone who''s replied so far. On Jul 20, 7:33 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 20 Jul 2008, at 19:04, Gearóid O''Ceallaigh wrote: > > > > > If its enlightenment you seek, then I''d suggest reading the entire > > post. Regardless, here is the code if you couldn''t figure it out: > > Shot in the dark: does your join table between bands and users have an > id column ? It shouldn''t. If rails is just doing a select * then the > id from the join table could easily clobber the id from the users table. > > Fred > > > band.rb > > *********************************************************** > > class Band < ActiveRecord::Base > > has_and_belongs_to_many :gigs > > has_and_belongs_to_many :users > > > def self.get_bands > > find (:all, :order => ''band_name'') > > end > > > end > > > user.rb > > ********************************************** > > > class User < ActiveRecord::Base > > has_and_belongs_to_many :bands > > > def self.per_page > > 50 > > end > > > has_many :moderatorships, :dependent => :destroy > > has_many :forums, :through => :moderatorships, :order => > > "#{Forum.table_name}.name" > > > has_many :posts > > has_many :topics > > has_many :monitorships > > has_many :monitored_topics, :through => :monitorships, :conditions > > => ["#{Monitorship.table_name}.active = ?", true], :order => > > "#{Topic.table_name}.replied_at desc", :source => :topic > > > validates_presence_of :login, :email > > validates_length_of :login, :minimum => 2 > > > with_options :if => :password_required? do |u| > > u.validates_presence_of :password_hash > > u.validates_length_of :password, :minimum => 5, :allow_nil > > => true > > u.validates_confirmation_of :password, :on => :create > > u.validates_confirmation_of :password, :on => :update, :allow_nil > > => true > > end > > > # names that start with #s really upset me for some reason > > validates_format_of :login, :with => /^[a-z]{2}(?:\w+)?$/i > > > # names that start with #s really upset me for some reason > > validates_format_of :display_name, :with => /^[a-z]{2}(?:[.''\- > > \w ]+)?$/i, :allow_nil => true > > > validates_format_of :email, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+ > > [a-z]{2,})$/i, :message => "Please check the e-mail > > address"[:check_email_message] > > > validates_uniqueness_of :email > > validates_uniqueness_of :login, :case_sensitive => false > > validates_uniqueness_of :display_name, :openid_url, :case_sensitive > > => false, :allow_nil => true > > before_validation { |u| u.display_name = u.login if > > u.display_name.blank? } > > # first user becomes admin automatically > > before_create { |u| u.admin = u.activated = true if User.count => > 0 } > > before_save { |u| u.email.downcase! if u.email } > > format_attribute :bio > > > attr_reader :password > > > attr_protected > > :admin > > , :posts_count > > , :login > > , :created_at, :updated_at, :last_login_at, :topics_count, :activated > > > def self.currently_online > > User.find(:all, :conditions => ["last_seen_at > ?", > > Time.now.utc-5.minutes]) > > end > > > # we allow false to be passed in so a failed login can check > > # for an inactive account to show a different error > > def self.authenticate(login, password, activated=true) > > find_by_login_and_password_hash_and_activated(login, > > Digest::SHA1.hexdigest(password + PASSWORD_SALT), activated) > > end > > > def self.search(query, options = {}) > > with_scope :find => { :conditions => > > build_search_conditions(query) } do > > options[:page] ||= nil > > paginate options > > end > > end > > > def self.build_search_conditions(query) > > query && [''LOWER(display_name) LIKE :q OR LOWER(login) LIKE :q'', > > {:q => "%#{query}%"}] > > end > > > def password=(value) > > return if value.blank? > > write_attribute :password_hash, Digest::SHA1.hexdigest(value + > > PASSWORD_SALT) > > @password = value > > end > > > def openid_url=(value) > > write_attribute :openid_url, value.blank? ? nil : > > OpenIdAuthentication.normalize_url(value) > > end > > > def reset_login_key(rehash = true) > > # this is not currently honored > > self.login_key_expires_at = Time.now.utc+1.year > > self.login_key = Digest::SHA1.hexdigest(Time.now.to_s + > > password_hash.to_s + rand(123456789).to_s).to_s if rehash > > end > > > def reset_login_key!(rehash = true) > > reset_login_key(rehash) > > save! > > login_key > > end > > > def active_login_key > > reset_login_key!(login_key.blank?) > > end > > > def moderator_of?(forum) > > moderatorships.count("#{Moderatorship.table_name}.id", :conditions > > => [''forum_id = ?'', (forum.is_a?(Forum) ? forum.id : forum)]) == 1 > > end > > > def to_xml(options = {}) > > options[:except] ||= [] > > options[:except] << :email << :login_key << :login_key_expires_at > > << :password_hash << :openid_url << :activated << :admin > > super > > end > > > def password_required? > > openid_url.nil? > > end > > > def update_posts_count > > self.class.update_posts_count id > > end > > > def self.update_posts_count(id) > > User.update_all [''posts_count = ?'', Post.count(:id, :conditions => > > {:user_id => id})], [''id = ?'', id] > > end > > end > > > On Jul 19, 12:17 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > > wrote: > >> On 18 Jul 2008, at 23:42, Gearóid O''Ceallaigh wrote: > > >>> I think my relationships may be set up incorrectly but I''m not sure > >>> why. I guess I''d be best to give a bit of background. > > >> If you thing that''s the case, then maybe showing us how you''re > >> setting > >> them up would enlighten everyone. > > >> Fred > > >>> I''m setting up a musicians network for my local area. There are > >>> bands > >>> and users as described above, and these are linked with the table > >>> "bands_users" which has three fields: id, band_id, and user_id. I am > >>> also working off the Beast forum so I thought it would be as easy to > >>> use their user table and configuration since its already set up. > > >>> So, what I find strange is that, when I want to return the user id > >>> like above, I must use "user.user_id". I believe I should be able to > >>> simply state user.id but doing so returns the value from "id" (note: > >>> NOT user_id - just the unique id from the bands_users table). > > >>> I also have a similar relationship set up between bands and gigs, > >>> but > >>> this works as desired. > > >>> Does anyone have any ideas why this would be messing up? Would the > >>> fact that I''m taking the data from the Beast Forum''s users be > >>> causing > >>> problems. Any help, as ever, is appreciated. > > >>> On Jul 17, 9:45 pm, Dejan Dimic <dejan.di...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >>>> the line > > >>>> <% if user.user_id == @current_user.id %> > > >>>> should be > > >>>> <% if user.id == @current_user.id %> > > >>>> On Jul 17, 10:10 pm, Gearóid O''Ceallaigh <gearoi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > >>>> wrote: > > >>>>> I''m relatively new to rails and am setting up a system which > >>>>> contains > >>>>> bands and users. Both bands and users has and belongs to many of > >>>>> the > >>>>> other. > > >>>>> In the bands "show" view, I want to list all the users within a > >>>>> band. > >>>>> I do this by using the code: > > >>>>> <% for user in @band.users %> > > >>>>> and iterating through the results. > > >>>>> However, I want to display a special div if the current user''s id > >>>>> (stored in the session variabel "@current_user.id") is within the > >>>>> returned results. I have unsuccessfully tried doing the following: > > >>>>> <% for user in @band.users %> > > >>>>> Member name:<%=h user.login %> <br> > > >>>>> <% if user.user_id == @current_user.id %> > >>>>> *DISPLAY SPECIAL DIV* > >>>>> <% end %> > > >>>>> <% end %> > > >>>>> I can''t understand why this isn''t working. Can anyone advise me on > >>>>> this?--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@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 -~----------~----~----~----~------~----~------~--~---
That works perfectly - Thanks a lot!! On 28 Jul, 17:21, Jeff <cohen.j...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Jul 27, 12:59 pm, Gearóid O''Ceallaigh <gearoi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > What I''d like to is to perform a check to see if a user is in a band > > within a controller. I presume I should put something along the > > following in the bands controller: > > > def is_member_in_band > > return if admin? > > for user in @band.users > > if user.id == @current_user.id > > return true > > end > > end > > end > > > Is there way of simply checking if the collection of users in > > @band.users contains the current user''s id without having to loop > > through them? > > This returns the user object if it exists: > > @band.users.find_by_id(@current_user.id) > > It will return nil if the user is not part of the band not found. > > Does this help? > > Jeff > purpleworkshops.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-/JYPxA39Uh5TLH3MbocFFw@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 -~----------~----~----~----~------~----~------~--~---