Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7812341
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T04:28:24+00:00 2026-06-02T04:28:24+00:00

i have a lessons table and a tags table. i associate both of the

  • 0

i have a lessons table and a tags table. i associate both of the them using a has_many :through relationship and my middle table is tags_relationship.rb

class Lesson < ActiveRecord::Base
  attr_accessible :title, :desc, :content, :tag_name
  belongs_to :user

  has_many  :tag_relationships
  has_many  :tags, :through => :tag_relationships
end

class Tag < ActiveRecord::Base
  attr_accessible :name

  has_many :tag_relationships
  has_many :lessons, :through => :tag_relationships
end

in one of my views, im trying to create a a virtual attribute. i have…

    <div class="tags">
        <%= f.label :tag_name, "Tags" %>
        <%= f.text_field :tag_name, data: { autocomplete_source: tags_path} %>
    </div>

but my lessons table doesn’t have that attribute, tag_name, so it calls my method instead

    def tag_name
       ????????
    end

    def tag_name=(name)
        self.tag = Tag.find_or_initialize_by_name(name) if name.present?
    end

however im not sure what to put inside the ????????. im trying to refer the :name attribute inside my tags table.

back then i used a has_many and belongs_to relationship. my lesson belonged to a tag (which was wrong) but i was able to write…

tag.name

and it worked. but since its a has_many :through now, im not sure. i tried using tags.name, Lessons.tags.name, etc but i cant seem to get it to work. how can i refer to the tags table name attribute? thank you

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T04:28:25+00:00Added an answer on June 2, 2026 at 4:28 am

    Apologize for my bad english.

    When your Lesson was belonged to Tag lesson had only one tag, so your code was right. But now Lesson has many Tags, and it is collection (array in simple words). So, your setter must be more complex:

    def tag_names=(names)
      names = if names.kind_of? String
        names.split(',').map{|name| name.strip!; name.length > 0 ? name : nil}.compact
      else
        names
      end
    
      current_names = self.tags.map(&:name) # names of current tags
      not_added = names - current_names # names of new tags
      for_remove = current_names - names # names of tags that well be removed
    
      # remove tags
      self.tags.delete(self.tags.where(:name => for_remove))
      # adding new
      not_added.each do |name|
        self.tags << Tag.where(:name => name).first || Tag.new(:name => name)
      end
    end
    

    And getter method should be like this:

    def tag_names
      self.tags.map(&:name)
    end
    

    BTW, finders like find_by_name are deprecated. You must use where.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to sort through a table (Lessons) and group them by Subject(A
I have a table that includes some student group name, lesson time, day names
I have dates in my script related to some teaching lessons. Each lesson has
In Django (1.0.2), I have 2 models: Lesson and StatLesson. class Lesson(models.Model): contents =
I have two tables: Lessons and Pages lessons has_many pages pages belongs_to lessons When
I have some code that downloads lessons as JSON, parses them, and puts them
hy! I have a Json String: {responseData:{days:[{date:1289430000,lessons:[{lesson:3,classname:XXXX,oldTeacher:RUMET,newTeacher:JAKOB,oldSubject:0AM,newSubject:0AM,oldRoom:104,newRoom:104 ,comment:},{lesson:4,classname:XXXX,oldTeacher:RUMET,newTeacher:JAKOB,oldSubject:0AM,newSubject:0APH,oldRoom:104,newRoom:107 ,comment:Verlegtvon},{lesson:8,classname:XXXX,oldTeacher:JAKOB,newTeacher:,oldSubject:0APH,newSubject:,oldRoom:107,newRoom: ,comment:Entfall}]},{date:1289516400,lessons:[{lesson:1,classname:XXXX,oldTeacher:KAIS,newTeacher:,oldSubject:0RW1,newSubject:,oldRoom:107,newRoom: ,comment:Entfall},{lesson:2,classname:XXXX,oldTeacher:KAIS,newTeacher:TRAUN,oldSubject:0RW1,newSubject:0BO,oldRoom:107,newRoom:107 ,comment:}]},{date:1289948400,lessons:[{lesson:5,classname:XXXX,oldTeacher:KIES,newTeacher:,oldSubject:0RK,newSubject:,oldRoom:107,newRoom: ,comment:Entfall}]},{date:1290121200,lessons:[{lesson:6,classname:XXXX,oldTeacher:KIES,newTeacher:,oldSubject:0RK,newSubject:,oldRoom:107,newRoom: ,comment:Entfall}]}]},responseDetails:null,responseStatus:200}
I have a table course chapter lessons 2 Chapter1 3,4 2 Chapter2 5,10,9,6,8 2
I have the following entities: namespace NhLists { public class Lesson { public virtual
In my app, I have the following @OneToMany relationship: @Entity public class ReservedLessons extends

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.