Rails App Has Trouble with Inter-Model Saving

Rails app has trouble with inter-model saving

The problem is here:

self.robots_tags.robots_meta = tag

self.robots_tags is a collection of objects defined by has_many :robots_tags, and you're attempting to assign a specific attribute to that entire collection. You can't do this. If you want to assign to an attribute on a specific object, you have to either iterate over the collection, or select a specific object from the collection via first or last or any of the other Enumerable methods.

Rails models not talking with each other. Not sure how to add records to one model from within another

You should be using self.robots_tags.create(...) to create a new RobotTag object.

Assuming your RobotTag has a tag attribute, it might look something like this:

downloaded_tags.each do |t|
self.robot_tags.create(:tag => t)
end

Web-scraping Rails App Getting Over-Modelled?

The "right" number of models and associations depends on your use cases and constraints. If database space is at a premium, database normalization might make more sense. If you want faster lookups, denormalization might make more sense. If you need to optimize certain kinds of lookups, arrange your models and relations for that. All of this said, if you are just prototyping, don't worry too much right now -- start with something that makes sense and see what happens.

The way you described (a many to many relationship) sounds fine to me if you want to be able to lookup in both directions:

  1. for a meta tag first and then find the associated sites
  2. for a site first and then find the associated meta tags

(Note: don't forget to add your indexes.)

By the way, in Rails, for a many to many join table, the Rails convention is to alphabetize the two table names before sticking them together. So it would be "meta_tags_sites" not "sites_meta_tags" by default. See the "has_and_belongs_to_many" section in A Guide to Active Record Associations.

Some Instance Methods Are Undefined in acts_as_taggable

tagged_with is a class method of Post, added by the acts_as_taggable_on gem.

@posts in your code is an instance of ActiveRecord::Relation, not the Post class itself or any instance of it.

There is no tag_list_on instance method in ActiveRecord::Relation, hence the error.

tagged_with says it returns

...a scope of objects that are tagged with the specified tags.

tag_list_on and set_tag_list_on are instance methods of the Post class, added by the acts_as_taggable gem.

You need to call tag_list_on and set_tag_list_on for each instance of @posts

user_tags = []
@posts.each do |post|
user_tags = user_tags + post.tag_list_on(:tags)
end
custom_tags = user_tags.uniq - params[:tags]

# ...

Rails Conventions pluralisations

This question is fairly old but I stumbled across it looking for a way to handle the word staff(as in employees at a company) correctly.

The reason it pluralises with the 's' is because the plural of staff(as in wizards staff) is staffs(staves is also valid). So if you needed a table with different kinds of (wizards)staffs it is valid. However the plural of staff(as in people at a company) is staff.

Rails assumes that you are referring to the wizards staff in this instance. With words with only one meaning that don't have a separate plural, this works correctly:

[1] pry(main)> "sheep".pluralize == "sheep" 
=> true

How to fix this?

config/initializers/inflections.rb

ActiveSupport::Inflector.inflections do |inflect|
inflect.plural "staff", "staff"
end

This will tell rails that the plural of staff is staff and the naming conventions will be respected.

rails generate scaffold staff name:string description:text active:boolean

      invoke  active_record
create db/migrate/20220518125452_create_staff.rb
create app/models/staff.rb
invoke test_unit
create test/unit/staff_test.rb
create test/fixtures/staff.yml
invoke resource_route
route resources :staff
invoke inherited_resources_controller
create app/controllers/staff_controller.rb
invoke erb
create app/views/staff
create app/views/staff/index.html.erb
create app/views/staff/edit.html.erb
create app/views/staff/show.html.erb
create app/views/staff/new.html.erb
create app/views/staff/_form.html.erb
invoke test_unit
create test/functional/staff_controller_test.rb
invoke helper
create app/helpers/staff_helper.rb
invoke test_unit
create test/unit/helpers/staff_helper_test.rb
invoke assets
invoke coffee
create app/assets/javascripts/staff.js.coffee
invoke scss
create app/assets/stylesheets/staff.css.scss
invoke scss
identical app/assets/stylesheets/scaffolds.css.scss

The migration:

create_table :staff do |t|
t.string :name
t.text :description
t.boolean :active

t.timestamps
end


Related Topics



Leave a reply



Submit