Giter VIP home page Giter VIP logo

Comments (3)

nhocki avatar nhocki commented on July 29, 2024

What's the exception? You may need allow the mass-assignment of attachments.

And do you really need STI? For what you have, you may need a Polymorphic association instead of the STI (depending on how related the discussions and the posts really are).

Anyway, paste your exception, that'll make it easier to see what's going wrong.

from nested_form.

gaaady avatar gaaady commented on July 29, 2024

I'm not sure what I'm supposed to do.
I thought that because of the inheritance I should only modify _form if discussion. when i changed it the attachments weren't added.
then I added to discussion.rb
has_many :attachments, :dependent => :destroy accepts_nested_attributes_for :attachments, :reject_if => lambda { |a| a[:url].blank? }, :allow_destroy => true

but when showing a post I got :
The single-table inheritance mechanism failed to locate the subclass: 'Discussion'. This error is raised because the column 'type' is reserved for storing the class in case of inheritance. Please rename this column if you didn't intend it to be used for storing the inheritance class or overwrite Post.inheritance_column to use another column for that information.

Edit : I finally noticed the warning "WARNING: Can't mass-assign protected attributes: attachments_attributes"

Discussion and post should have the same data but different behavior.

from nested_form.

nhocki avatar nhocki commented on July 29, 2024

What's your database schema for the posts table? To use STI you'll need a type:string attribute. Then, if you're creating a Discussion, the type of that discussion should be discussion.

With that, you should be able to add the accepts_nested_attributes_for to the Post model.

About this Discussion and post should have the same data but different behavior.

I'm not sure, but I guess that if you have a _form partial under the app/views/discussions, rails will load that one instead of the posts/_form one.

But, if the behaviour is really different, I don't think the STI is the right approach. I would use Polymorphic Associations. That way you'll be able to have this:

Post has_many :attachments, :as => :holder
Discussion has_many :attachments, :as => :holder
Attachment belongs_to :holder, :polymorphic => true

And you'll be able to scale it easier without having non-related classes in the same table.

Hope this helps you... but if it doesn't, paste your Post database schema...

from nested_form.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.